Significant difference in execution speed between similar functions with nested loops

I compared the execution speed of two similar functions, f1 and f2, with f1 taking almost 1000 times longer than f2. Both functions include nested loops. Can anyone explain why there is such a significant difference in execution speed?

(I’m using Julia1.8.3, MacBook Pro with Apple M1 Pro CPU, and macOS Monterey version 12.6)

function f1()
    t = 12131
    for _ in 1:1000
        for _ in 1:t
            x = sin(1)
        end
    end
end


function f2(indexes)
    for _ in 1:1000
        for _ in indexes
            x = sin(1)
        end
    end
end

@time f1();
@time f1();  #  0.026695 seconds
println()

indexes = 1:12131
@time f2(indexes);
@time f2(indexes);  #   0.000004 seconds

one of them probably gets optimized away? use @code_llvm to check if it’s actually doing anything

1 Like

Hi Jling,

Thanks for the reply.
The result of the @code_llvm macro is as follows.
Does this mean that f2 is optimized?

(Sorry, I’m not familiar with this kind of output)

;  @ /path/to/loop.jl:1 within `f1`
define void @julia_f1_7022() #0 {
top:
;  @ /path/to/loop.jl:7 within `f1`
  ret void
}

;  @ /path/to/loop.jl:11 within `f2`
define void @julia_f2_7024([2 x i64]* nocapture nonnull readonly align 8 dereferenceable(16) %0) #0 {
top:
;  @ /path/to/loop.jl:16 within `f2`
  ret void
}

There’s some funny business with @time sometimes forcing recompiling, and sometimes not. I don’t know a good rule to know when it will happen.

julia> function f3(indexes=1:12131)
           for _ in 1:1000
               for _ in indexes
                   x = sin(1)
               end
           end
       end
f3 (generic function with 2 methods)

julia> @time f3()
       @time f3()
  0.014604 seconds
  0.019143 seconds

julia> @time f3(1:12131)
       @time f3(1:12131)
  0.014021 seconds
  0.015006 seconds

julia> indexes = 1:12131
       @time f3(indexes)
       @time f3(indexes)
  0.005615 seconds (160 allocations: 7.234 KiB, 99.83% compilation time)
  0.000008 seconds

julia> using BenchmarkTools

julia> @btime f3()
  3.000 ns (0 allocations: 0 bytes)

@uniment
Thanks for your reply.

I was surprised by the difference between @btime and @time.
So, basically, it seems that the difference comes from the @time’s recompiling time.
(I also checked the outputs of @code_llvm of f3(), f3(1:12131) and f3(indexes).

Thank you very much for your valuable comment.

1 Like