What is happening? Why these simple codes slow down Julia 30 times?

mm(i::Int)=(a::Int,b::Int)->a*b

#test1
@time begin
    gg=mm(10)
    for i=1:1000_0000
        gg(10,10)
    end
end

 # 0.091843 seconds

#test2
@time begin
    for i=1:1000_0000
        mm(10)(10,10)
    end
end

# 0.003385 seconds

test2 is faster 30 times than test1. But in my view, test2 generate a new function each time in the loop, the new function needs time to be compiled, it should be slower than test1.

What happened?

I suspect that you will see different timings if you put your tests inside functions or, if you stay in the global scope, use const gg = mm(10).

4 Likes

I see, @time begin ... end, this block is still in global, I thought it is in local scope.
Thank you so much.

1 Like

Maybe this is a cultural thing, but I would warn against grouping digits like that. 1000_0000 == 10_000_000, but instinctively most of us (I suspect) will read the former as 1_000_000.

3 Likes

This is a good point. I can see why someone would think begin ... end is local, but it is (I think) the only of the three similar constructs that remains global in this situation, both do ... end and let ... end introduce a local scope.

3 Likes