# Simple code run @time 1.7, 1.8, 1.9 difference

**URL:** <https://discourse.julialang.org/t/simple-code-run-time-1-7-1-8-1-9-difference/98026>\
**Category:** New to Julia\
**Created:** [April 28, 2023, 8:28am UTC](https://discourse.julialang.org/t/simple-code-run-time-1-7-1-8-1-9-difference/98026 "2023-04-28T08:28:42Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![kimuh](https://avatars.discourse-cdn.com/v4/letter/k/a88e4f/32.png) [@kimuh](https://discourse.julialang.org/u/kimuh)\
**Post date:** [April 28, 2023, 8:28am UTC](https://discourse.julialang.org/t/simple-code-run-time-1-7-1-8-1-9-difference/98026/1 "2023-04-28T08:28:42Z")

</div>

```julia
function sum_n(n)
	s = 0
	for i in 1:n
		s += i
	end
	return s
end

@time sum_n(1_000_000_000)

```

On Windows 11 x64

1.7.3 : 0.000001 seconds  
1.8.5 : 2.033023 seconds  
1.9 rc2 : 5.878953 seconds

Did I do something wrong?

P.S\> Sample code from ‘Julia for Data Analysis’ chapter 1.

---

<div class="post-metadata">

**Author:** ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)\
**Post date:** [April 28, 2023, 8:55am UTC](https://discourse.julialang.org/t/simple-code-run-time-1-7-1-8-1-9-difference/98026/2 "2023-04-28T08:55:38Z")

</div>

Seeing the same timing, though running on 1.9 without the `@time` macro it is instant (or at least not a few seconds).

Introducing `f() = sum_n(1_000_000_000)` and running `@code_llvm f()` we see that the compiler can simplify the call to a constant in all versions.

So it seems there is some change in the `@time` macro that makes the later versions not allow that optimization when timing, while 1.7 does.

---

<div class="post-metadata">

**Author:** ![sparrowhawk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sparrowhawk/32/9148_2.png) [@sparrowhawk](https://discourse.julialang.org/u/sparrowhawk)\
**Post date:** [April 28, 2023, 9:16am UTC](https://discourse.julialang.org/t/simple-code-run-time-1-7-1-8-1-9-difference/98026/3 "2023-04-28T09:16:50Z")

</div>

Perhaps to do with compiling the first time for the first run? Though from what I understand this TTFX is dealt with in 1.9x. Have you tried using BenchmarkTools? See here: [Home · BenchmarkTools.jl](https://juliaci.github.io/BenchmarkTools.jl/stable/)

---

<div class="post-metadata">

**Author:** ![kimuh](https://avatars.discourse-cdn.com/v4/letter/k/a88e4f/32.png) [@kimuh](https://discourse.julialang.org/u/kimuh)\
**Post date:** [April 28, 2023, 9:19am UTC](https://discourse.julialang.org/t/simple-code-run-time-1-7-1-8-1-9-difference/98026/4 "2023-04-28T09:19:00Z")

</div>

> running on 1.9 without the `@time` macro it is instant (or at least not a few seconds).

confirmed.  
It is also same with 1.8.5, with no ‘@time’ macro, it is instant, too.  
I don’t understand `@code_llvm f()` things, but even to me, it looks like @time macro in later version does not allow optimized behavior.

Thank you for detailed explanation.

---

<div class="post-metadata">

**Author:** ![kimuh](https://avatars.discourse-cdn.com/v4/letter/k/a88e4f/32.png) [@kimuh](https://discourse.julialang.org/u/kimuh)\
**Post date:** [April 28, 2023, 9:21am UTC](https://discourse.julialang.org/t/simple-code-run-time-1-7-1-8-1-9-difference/98026/5 "2023-04-28T09:21:19Z")

</div>

It is same first run with 1.7, but different run time.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [April 28, 2023, 9:34am UTC](https://discourse.julialang.org/t/simple-code-run-time-1-7-1-8-1-9-difference/98026/6 "2023-04-28T09:34:15Z")

</div>

There have been changes to `@time` since 1.7 to prevent the compiler from beating benachmarks in certain situations, so in 1.7 the loop isn’t actually executed but replaced with the return value by LLVM.

---

<div class="post-metadata">

**Author:** ![jakobnissen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakobnissen/32/13477_2.png) [@jakobnissen](https://discourse.julialang.org/u/jakobnissen)\
**Post date:** [April 28, 2023, 11:01am UTC](https://discourse.julialang.org/t/simple-code-run-time-1-7-1-8-1-9-difference/98026/7 "2023-04-28T11:01:27Z")

</div>

I don’t think this is correct. What’s actually happening is that `@time` appears to inhibit normal optimisation on 1.9. You can see that on all versions of Julia, the compiler figures out the analytical solution to the problem. Yet somehow, `@time` makes it slow in Julia 1.9.

It appears to be solved on recent master though.

Edit: I’ve made an issue: [`@time` inhibits optimisation on 1.9 · Issue #49554 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/49554)

Edit2: This is a known regression, and a fix is being backported: [Backport to 1.9: Improve performance of global code by emitting fewer atomic barriers. by maleadt · Pull Request #49411 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/49411)

---

<div class="post-metadata">

**Author:** ![kimuh](https://avatars.discourse-cdn.com/v4/letter/k/a88e4f/32.png) [@kimuh](https://discourse.julialang.org/u/kimuh)\
**Post date:** [April 28, 2023, 1:05pm UTC](https://discourse.julialang.org/t/simple-code-run-time-1-7-1-8-1-9-difference/98026/8 "2023-04-28T13:05:58Z")

</div>

You are right. It is a duplicate of [[Spurious performance regression in Julia 1.8 vs 1.7 for `@time` in top-level-scope · Issue #47561 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/47561)].

Actually, #47561 was opened almost 5 months ago with the same sample code by the writer of the book “Julia for Data Analysis”, from which I got sample code in this question.

Pleased to know that fix is already merged.

Thank for letting me know the issue.
