Below are two tests, in the first a simple polynomial is "hard-coded", in
the second it is passed as a function. I would expect the two to be
equivalent, but the second case is significantly faster. Can anybody
explain what is going on? @code_warntype doesn't show anything that would
explain it?
function test1(N)
r = 0.234; s = 0.0
for n = 1:N
s += r^3 + r^5
end
return s
end
function test2(N, f1)
r = 0.234; s = 0.0
for n = 1:N
s += f1(r)
end
return s
end
g1(r) = r^3 + r^5
test1(10)
test2(10, g1)
println("Test1: hard-coded functions")
@time test1(1_000_000)
@time test1(1_000_000)
@time test1(1_000_000)
println("Test2: pass functions")
@time test2(1_000_000, g1)
@time test2(1_000_000, g1)
@time test2(1_000_000, g1)
# $ julia5 weird_test2.jl
# Test1: hard-coded functions
# 0.086683 seconds (4.00 M allocations: 61.043 MB, 50.75% gc time)
# 0.142487 seconds (4.00 M allocations: 61.035 MB, 76.91% gc time)
# 0.025388 seconds (4.00 M allocations: 61.035 MB, 4.28% gc time)
# Test2: pass functions
# 0.000912 seconds (5 allocations: 176 bytes)
# 0.000860 seconds (5 allocations: 176 bytes)
# 0.000846 seconds (5 allocations: 176 bytes)