Is there some reason why a function defined inside a let-block is slower compared to defining the same function in global scope?
e.g. below recursive Fibonacci (from the old Julia microbenchmarks) is 10 times slower if defined inside let-block. Is some optimization step not applied inside the let-block or am I just missing something?
With Julia 1.10 REPL
julia> let
fib(n) = n < 2 ? n : fib(n-1) + fib(n-2)
@time a=fib(35)
@time b=fib(35)
@time c=fib(35)
@time d=fib(35)
@time e=fib(35)
@time f=fib(35)
end
0.407809 seconds (29.88 k allocations: 536.734 KiB, 0.82% compilation time)
0.411220 seconds (28.66 k allocations: 448.125 KiB)
0.392723 seconds (28.66 k allocations: 448.125 KiB)
0.408793 seconds (28.66 k allocations: 448.125 KiB)
0.407156 seconds (28.66 k allocations: 448.125 KiB)
0.415548 seconds (28.66 k allocations: 448.125 KiB)
9227465
julia> begin
fib(n) = n < 2 ? n : fib(n-1) + fib(n-2)
@time a=fib(35)
@time b=fib(35)
@time c=fib(35)
@time d=fib(35)
@time e=fib(35)
@time f=fib(35)
end
0.035875 seconds (941 allocations: 63.344 KiB, 5.92% compilation time)
0.033755 seconds (9 allocations: 400 bytes)
0.033742 seconds (9 allocations: 400 bytes)
0.033796 seconds (9 allocations: 400 bytes)
0.033766 seconds (9 allocations: 400 bytes)
0.033747 seconds (9 allocations: 400 bytes)
9227465