# Huge difference in performance between two programmes

**URL:** <https://discourse.julialang.org/t/huge-difference-in-performance-between-two-programmes/60116>\
**Category:** Performance\
**Created:** [April 27, 2021, 4:53pm UTC](https://discourse.julialang.org/t/huge-difference-in-performance-between-two-programmes/60116 "2021-04-27T16:53:12Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![encendre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/encendre/32/21893_2.png) [@encendre](https://discourse.julialang.org/u/encendre)\
**Post date:** [April 27, 2021, 4:53pm UTC](https://discourse.julialang.org/t/huge-difference-in-performance-between-two-programmes/60116/1 "2021-04-27T16:53:12Z")

</div>

I’m sorry for the not very descriptive title but I couldn’t find a better way to phrase it

I have two similar programmes with a huge difference in performance

```julia
let
	function useless()
	end
	
	@time let
		s = 0
		for n in 1:100000
			s += n^10
		end
		s
	end
end

```

```julia
let
	@time let
		s = 0
		for n in 1:100000
			s += n^10
		end
		s
	end
end

```

The first gives me `0.221958 seconds (498.22 k allocations: 9.129 MiB, 3.08% gc time, 1.90% compilation time)` and the second fives me `0.000540 seconds`

The difference in performance is the same as when using global variables where normally everything is hard scoped with the `let` keyword.

So what is the source of the problem and how to avoid this situation ?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [April 27, 2021, 5:01pm UTC](https://discourse.julialang.org/t/huge-difference-in-performance-between-two-programmes/60116/2 "2021-04-27T17:01:05Z")

</div>

> [@encendre](#):
>
> So what is the source of the problem and how to avoid this situation ?

The difference you are seeing seems to be spurious, it’s just one-time compilation/startup costs. If I use `@btime` from [BenchmarkTools.jl](https://github.com/JuliaCI/BenchmarkTools.jl) instead of `@time`, it reports exactly the same time (389µs) in both cases. (The `@btime` macro performs the measurement several times and reports the smallest, and it is generally far more reliable than `@time` for drawing conclusions about performance.)

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [April 27, 2021, 5:05pm UTC](https://discourse.julialang.org/t/huge-difference-in-performance-between-two-programmes/60116/3 "2021-04-27T17:05:33Z")

</div>

Something does seem weird given that `@time` reports only `1.9%` compilation time. Not sure what to do about it though.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [April 27, 2021, 5:11pm UTC](https://discourse.julialang.org/t/huge-difference-in-performance-between-two-programmes/60116/4 "2021-04-27T17:11:47Z")

</div>

> [@Oscar\_Smith](#):
>
> Something does seem weird given that `@time` reports only `1.9%` compilation time.

In Julia 1.6, I just get “0.160966 seconds (498.20 k allocations: 9.128 MiB)” with nothing about compilation time.

---

<div class="post-metadata">

**Author:** ![encendre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/encendre/32/21893_2.png) [@encendre](https://discourse.julialang.org/u/encendre)\
**Post date:** [April 27, 2021, 5:16pm UTC](https://discourse.julialang.org/t/huge-difference-in-performance-between-two-programmes/60116/5 "2021-04-27T17:16:12Z")

</div>

I guess `@btime` has to do some magic stuff that doesn’t allow you to see the problem. But when I found the problem it was because a program that usually ran in one second was taking several minutes.

By multiplying the number of iterations of the loop by 100 and removing the `@time` the first program takes about a 10s while the second is instantaneous.

So the problem remains and is very similar to the performance problem of global variables

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [April 27, 2021, 5:20pm UTC](https://discourse.julialang.org/t/huge-difference-in-performance-between-two-programmes/60116/6 "2021-04-27T17:20:19Z")

</div>

Replacing the `let` block with a function is sufficient to completely eliminate the problem for me. That seems like it would also solve the issue in your actual program, since it’s easy to turn a bare `let` block into a function.

---

<div class="post-metadata">

**Author:** ![encendre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/encendre/32/21893_2.png) [@encendre](https://discourse.julialang.org/u/encendre)\
**Post date:** [April 27, 2021, 6:45pm UTC](https://discourse.julialang.org/t/huge-difference-in-performance-between-two-programmes/60116/7 "2021-04-27T18:45:56Z")

</div>

Yes, it solves the problem but I would like to understand why there is a problem

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [April 27, 2021, 7:02pm UTC](https://discourse.julialang.org/t/huge-difference-in-performance-between-two-programmes/60116/8 "2021-04-27T19:02:24Z")

</div>

Probably you should report an issue about that. Clearly something strange is happening there. The same happens with `begin...end` blocks:

start julia and:

```julia
julia> @time begin
               f() = nothing
                let
                       s = 0
                       for n in 1:100000
                               s += n^10
                       end
                       s
               end
       end
  0.142710 seconds (500.56 k allocations: 9.295 MiB, 4.71% gc time, 5.31% compilation time)
-5724124746025484656

julia> @time begin
               f() = nothing
                let
                       s = 0
                       for n in 1:100000
                               s += n^10
                       end
                       s
               end
       end
  0.146696 seconds (498.21 k allocations: 9.129 MiB)
-5724124746025484656

```

restart julia and:

```julia
julia> @time begin
               let
                       s = 0
                       for n in 1:100000
                               s += n^10
                       end
                       s
               end
       end
  0.000568 seconds
-5724124746025484656

```
