A very lazy and naïve question. Simple example,
case one,
if I set
a = 0.5
in the code and then compile and run.
Or, case two,
if I read in the value of a as 0.5
read (5,*) a
Are there performance difference between the two cases?
I mean if I set a=0.5 in the code instead of read in it, will the compiler optimize for a = 0.5? Or it does not matter?
Thanks much in advance!
[Clarification]
As @nicholaswogan asked, no the question have nothing to do with I/O in anyway. Sorry for the confusion.
I am just asking,
if I set a=0.5, will compiler optimize it for a=0.5 during compiling?
On the other hand, if a is read in as 0.5, then the value of a is only know at run time, so it seems there is no way a = 0.5 is optimized in anyway.
I am actually asking,
we usually read in the value of variable from like txt file, instead of setting them in the code. Because the former does not require recompile the code again and again, while the latter will require recompile the code again and again.
But does latter have some performance advantage because the value of values are know at compiling time?
gfortran -O2 -S produces assembler code without any function call (apart from *write*), apparently providing compiler-calculated value.
I was unable, however, to avoid calling powf function in any scenario where there was a variable instead of constant 4.25 as the base of exponentiation. I thought it might be optimized to use sqrt instead of powf.
If a is truly a constant, define it with the parameter value and there is a good chance the compiler will produce a constant; but I did not try it.
The original I/O question is so general only a general answer can be given without writing a book as a reply. I/O is slower, but is often needed when large amounts of data, or values that change between runs are required. Fortran is a compiled language; it is rare to want to have to recompile every time you change a value.
Thank you all @msz59@urbanjost@nicholaswogan !
As @nicholaswogan pointed out, I added clarification.
Sorry for the confusion. I am actually asking if the value of variable, say a=0.5, is known at compiling time, will compiler optimize for a=0.5? If so, it seems setting values in the code can be better than read in them from a file.
But I guess as @nicholaswogan pointed, things like a=0.5 known at compiling time or run time should not matter. Just very lazy to check so just ask here