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