Simple code run @time 1.7, 1.8, 1.9 difference

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.

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.

1 Like

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

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.

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

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.

1 Like

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

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

6 Likes

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

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.