On Mon, Dec 7, 2015 at 6:00 AM, <ele...@gmail.com> wrote: > You are making the assumption that `sin(.34567)` is a constant, but in Julia > technically sin is dependent on global state, specifically the rounding and > denormalisation modes > http://docs.julialang.org/en/release-0.4/stdlib/numbers/?highlight=rounding#Base.set_rounding. > It these change during the loop, then the answer may not be the same, and > like all global state Julia can't infer that. Whereas you have demonstrated > that you can tell Julia its a constant with the same value as calculated at > initialisation. >
Ref https://github.com/JuliaLang/julia/issues/9942 > > On Monday, December 7, 2015 at 7:59:08 PM UTC+10, Meik Hellmund wrote: >> >> >> Thank you for pointing out the problem with a global y and its type. >> I hope my new code example below is better. I want to point at >> the relative performance difference of the functions f and g. >> >> I am experimenting with Julia for a couple of weeks and I like it very >> much. >> In my experience, every language has a different borderline between >> "The programmer should think about optimal performance" and >> "Don't overoptimize, the compiler is smarter than you". >> >> So I just want to understand if Julia does >> constant expression evaluation at compile time or not >> >> I think julia 0.4.1 does not. My code: >> #------------------------------------- >> function stupidtest(f) >> for x in 1 : 10^8 >> f(x) >> end >> end >> >> f(x) = x + log(2.) * sin(.34567)^2 >> >> const z = log(2.) * sin(.34567)^2 >> g(x) = x + z >> >> @time( stupidtest(f) ) >> @time( stupidtest(g) ) >> #------- EOF------------------------------ >> >> $julia --optimize --inline=yes --check-bounds=no --math-mode=fast >> mycode.jl >> 2.869774 seconds (200.00 M allocations: 2.980 GB, 3.39% gc time) >> 1.855530 seconds (200.00 M allocations: 2.980 GB, 4.32% gc time) >> >> >> It also seems that I (with a C++/Fortran background) do not really >> understand >> some aspects of the language. Why does a loop with a function call >> need so much memory allocations? >> >> Any explanations are appreciated! >> >> Cheers, Meik >> >> >