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

**URL:** <https://discourse.julialang.org/t/what-is-happening-why-these-simple-codes-slow-down-julia-30-times/80529>\
**Category:** New to Julia\
**Created:** [May 5, 2022, 7:43am UTC](https://discourse.julialang.org/t/what-is-happening-why-these-simple-codes-slow-down-julia-30-times/80529 "2022-05-05T07:43:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brian1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian1/32/49250_2.png) [@Brian1](https://discourse.julialang.org/u/Brian1)\
**Post date:** [May 5, 2022, 7:43am UTC](https://discourse.julialang.org/t/what-is-happening-why-these-simple-codes-slow-down-julia-30-times/80529/1 "2022-05-05T07:43:51Z")

</div>

```julia
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?

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [May 5, 2022, 7:49am UTC](https://discourse.julialang.org/t/what-is-happening-why-these-simple-codes-slow-down-julia-30-times/80529/2 "2022-05-05T07:49:45Z")

</div>

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)`.

---

<div class="post-metadata">

**Author:** ![Brian1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian1/32/49250_2.png) [@Brian1](https://discourse.julialang.org/u/Brian1)\
**Post date:** [May 5, 2022, 7:54am UTC](https://discourse.julialang.org/t/what-is-happening-why-these-simple-codes-slow-down-julia-30-times/80529/3 "2022-05-05T07:54:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [May 5, 2022, 12:42pm UTC](https://discourse.julialang.org/t/what-is-happening-why-these-simple-codes-slow-down-julia-30-times/80529/4 "2022-05-05T12:42:46Z")

</div>

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`.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [May 5, 2022, 1:41pm UTC](https://discourse.julialang.org/t/what-is-happening-why-these-simple-codes-slow-down-julia-30-times/80529/5 "2022-05-05T13:41:34Z")

</div>

> [@Brian1](#):
>
> `@time begin ... end` , this block is still in global, I thought it is in local scope.

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.
