# Possible Performance Regression for Loops on 1.9?

**URL:** <https://discourse.julialang.org/t/possible-performance-regression-for-loops-on-1-9/93994>\
**Category:** Performance\
**Created:** [February 3, 2023, 3:17pm UTC](https://discourse.julialang.org/t/possible-performance-regression-for-loops-on-1-9/93994 "2023-02-03T15:17:39Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![jacobxk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacobxk/32/14405_2.png) [@jacobxk](https://discourse.julialang.org/u/jacobxk)\
**Post date:** [February 3, 2023, 3:17pm UTC](https://discourse.julialang.org/t/possible-performance-regression-for-loops-on-1-9/93994/1 "2023-02-03T15:17:39Z")

</div>

There is a function in Bogumił Kamiński’s book _Julia for Data Analysis_(page 7) with the purpose to show off the optimizations done by Julia compiler.

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

```

In the book, the code shown superb results in runtime, like 0.000001 seconds, where running the Julia v1.7. However, I tested the same function in both v1.8.3, v1.8.5 and v1.9.0, the results were not the case, which yielded about one second. Thus, I am wondering that what happened from v1.7 to v1.8 and above on the loops?

 ![Xnip2023-02-03_23-07-19](https://global.discourse-cdn.com/julialang/original/3X/b/6/b658e0937df44f12bb14c4fe69b5cd126670b42e.jpeg)

---

<div class="post-metadata">

**Author:** ![TheCedarPrince](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thecedarprince/32/17323_2.png) [@TheCedarPrince](https://discourse.julialang.org/u/TheCedarPrince)\
**Post date:** [February 3, 2023, 3:32pm UTC](https://discourse.julialang.org/t/possible-performance-regression-for-loops-on-1-9/93994/2 "2023-02-03T15:32:12Z")

</div>

Could you please re-run your function after using the package BenchmarkTools.jl? Like this:

```julia
using BenchmarkTools

@btime sum_n(1_000_000_000)

```

And post back the results?

P.S. Also, welcome to the Julia community! Thanks for bringing this up! 👋

---

<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:** [February 3, 2023, 3:32pm UTC](https://discourse.julialang.org/t/possible-performance-regression-for-loops-on-1-9/93994/3 "2023-02-03T15:32:43Z")

</div>

This is very odd. The `@code_native` is showing that the loop can be folded, but when run in the global scope, it’s running the loop.

---

<div class="post-metadata">

**Author:** ![filchristou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/filchristou/32/26760_2.png) [@filchristou](https://discourse.julialang.org/u/filchristou)\
**Post date:** [February 3, 2023, 3:33pm UTC](https://discourse.julialang.org/t/possible-performance-regression-for-loops-on-1-9/93994/4 "2023-02-03T15:33:37Z")

</div>

A small remark. First of all and as a rule of thumb, you should benchmark with `@time` on the second call of the function. That is because the first time you call the function it will also compile it and essentially `@time` will also include compilation time, which is something you typically are not interested in.  
Also using BenchmarkTools.jl as mentioned above will help a deal.

---

<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:** [February 3, 2023, 3:35pm UTC](https://discourse.julialang.org/t/possible-performance-regression-for-loops-on-1-9/93994/5 "2023-02-03T15:35:14Z")

</div>

This isn’t compilation time. Running `@time` repeatedly shows it taking 1.7 seconds, but running `@btime` on it takes nanoseconds (and this is easy to see since `@btime` runs faster than `@time` which shouldn’t be possible).

---

<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:** [February 3, 2023, 3:40pm UTC](https://discourse.julialang.org/t/possible-performance-regression-for-loops-on-1-9/93994/6 "2023-02-03T15:40:38Z")

</div>

Ok, this appears to be something weird `@time` is doing. just running `sum_n` is fast.

---

<div class="post-metadata">

**Author:** ![jacobxk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacobxk/32/14405_2.png) [@jacobxk](https://discourse.julialang.org/u/jacobxk)\
**Post date:** [February 3, 2023, 3:43pm UTC](https://discourse.julialang.org/t/possible-performance-regression-for-loops-on-1-9/93994/7 "2023-02-03T15:43:42Z")

</div>

Thanks for the info. Here is the results.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/f/6/f6ac379d6e0dfcb1d05fb66aec8fc1353b3857fa.png)

While there were almost equivalent performance in terms of running `@btime`, but still the difference about a nanosecond. In the book, Bogumił explained that the consecutive sum would be optimized as to `n(n+1)/2`. I am not sure it is still the case in v1.8.0 or above. How can I check it, given that I don’t know assembly language.

---

<div class="post-metadata">

**Author:** ![jacobxk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacobxk/32/14405_2.png) [@jacobxk](https://discourse.julialang.org/u/jacobxk)\
**Post date:** [February 3, 2023, 3:46pm UTC](https://discourse.julialang.org/t/possible-performance-regression-for-loops-on-1-9/93994/8 "2023-02-03T15:46:43Z")

</div>

I checked with a much larger number, and the result implies that the `n(n+1)/2` still the case?

 ![image](https://global.discourse-cdn.com/julialang/original/3X/0/2/026d3cec389e0e49eb7aabce1c3ca558fdbf824d.png)

---

<div class="post-metadata">

**Author:** ![jacobxk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacobxk/32/14405_2.png) [@jacobxk](https://discourse.julialang.org/u/jacobxk)\
**Post date:** [February 3, 2023, 3:48pm UTC](https://discourse.julialang.org/t/possible-performance-regression-for-loops-on-1-9/93994/9 "2023-02-03T15:48:30Z")

</div>

Yes, if I conduct `@time sum_n(1_000_000_000_000)`, there is a demanding CPU loading and no expected results shown after a long time.

---

<div class="post-metadata">

**Author:** ![TheCedarPrince](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thecedarprince/32/17323_2.png) [@TheCedarPrince](https://discourse.julialang.org/u/TheCedarPrince)\
**Post date:** [February 3, 2023, 3:56pm UTC](https://discourse.julialang.org/t/possible-performance-regression-for-loops-on-1-9/93994/10 "2023-02-03T15:56:56Z")

</div>

Hm. Thanks for posting this back! This is helpful.

I cannot explain why it is apparently slower by a single nanosecond – perhaps a regression somewhere on 1.9 that hasn’t been caught yet? – but I’d say the speeds are still comparable as you mentioned. I am sure a speed hacker like @Oscar_Smith or @Elrod could probably comment more. Just looking at this though from my perspective, this does seem to be a minor regression on 1.9.

P.S. I updated the title of your post to make it a bit more precise for other Julians to see/understand.

---

<div class="post-metadata">

**Author:** ![jacobxk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacobxk/32/14405_2.png) [@jacobxk](https://discourse.julialang.org/u/jacobxk)\
**Post date:** [February 3, 2023, 3:58pm UTC](https://discourse.julialang.org/t/possible-performance-regression-for-loops-on-1-9/93994/11 "2023-02-03T15:58:27Z")

</div>

Thanks and please go ahead.

---

<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:** [February 3, 2023, 5:33pm UTC](https://discourse.julialang.org/t/possible-performance-regression-for-loops-on-1-9/93994/12 "2023-02-03T17:33:27Z")

</div>

There was some change to time to avoid constant folding and similar instances of the compiler defeating the benchmark iirc recently

---

<div class="post-metadata">

**Author:** ![vchuravy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vchuravy/32/8_2.png) [@vchuravy](https://discourse.julialang.org/u/vchuravy)\
**Post date:** [February 3, 2023, 6:23pm UTC](https://discourse.julialang.org/t/possible-performance-regression-for-loops-on-1-9/93994/13 "2023-02-03T18:23:02Z")

</div>

The issue is [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)

There is a proposed fix, but it is not yet ready for prime time.
