Also if, inside the function, you keep the type of `y` stable by y=0.0 
Julia will optimise away the second loop entirely, and do no allocations 
for the first loop.

On Monday, December 7, 2015 at 1:42:34 PM UTC+10, [email protected] wrote:
>
>
>
> On Monday, December 7, 2015 at 1:11:05 PM UTC+10, Meik Hellmund wrote:
>>
>> Hi,
>>
>> It seems that julia does not "optimize away" constant expressions in 
>> functions:
>>
>> julia> f(x)=x+2-2
>> f (generic function with 1 method)
>>
>> julia> f(1.e-18)
>> 0.0
>>
>> This is of course correct in Float64 arithmetics. 
>> But I wonder: In languages like Fortran and C the result of such code 
>>  depends on the "optimization flags" used when compiling. 
>> With optimization, the compiler would reduce the function to f(x)=x.
>> Is there something comparable in Julia?
>>
>> Another test. Compare
>>     f(x)=x+sin(.34567)
>>
>> to
>>
>>     const sn=sin(.34567)
>>     g(x)=x+sn
>>
>>
>> julia> y=0; @time(for i in 1:10^9; y=f(y); end)
>>  22.489526 seconds (2.00 G allocations: 29.802 GB, 3.50% gc time)
>>
>> julia> y=0; @time(for i in 1:10^9; y=g(y); end)
>>  16.268512 seconds (1000.00 M allocations: 14.901 GB, 2.61% gc time)
>>
>
> Your `y` is a global variable, that prevents optimisations, putting the 
> above in functions gives me no material difference between the time or 
> allocations.  See 
> http://julia.readthedocs.org/en/latest/manual/performance-tips/
>  
>
>>
>>
>> It looks like Julia does not optimize even the simplest constant 
>> expressions so that they are  evaluated  only once. 
>> And why, by all means, does it allocate so much memory for that?
>>  Is there something I overlook?
>>
>>  Best wishes, 
>> Meik
>>
>>

Reply via email to