# Significant difference in execution speed between similar functions with nested loops

**URL:** <https://discourse.julialang.org/t/significant-difference-in-execution-speed-between-similar-functions-with-nested-loops/95710>\
**Category:** Performance\
**Tags:** question\
**Created:** [March 8, 2023, 4:42am UTC](https://discourse.julialang.org/t/significant-difference-in-execution-speed-between-similar-functions-with-nested-loops/95710 "2023-03-08T04:42:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![aomidro](https://avatars.discourse-cdn.com/v4/letter/a/6a8cbe/32.png) [@aomidro](https://discourse.julialang.org/u/aomidro)\
**Post date:** [March 8, 2023, 4:42am UTC](https://discourse.julialang.org/t/significant-difference-in-execution-speed-between-similar-functions-with-nested-loops/95710/1 "2023-03-08T04:42:21Z")

</div>

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)

```julia
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

```

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [March 8, 2023, 4:43am UTC](https://discourse.julialang.org/t/significant-difference-in-execution-speed-between-similar-functions-with-nested-loops/95710/2 "2023-03-08T04:43:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![aomidro](https://avatars.discourse-cdn.com/v4/letter/a/6a8cbe/32.png) [@aomidro](https://discourse.julialang.org/u/aomidro)\
**Post date:** [March 8, 2023, 4:53am UTC](https://discourse.julialang.org/t/significant-difference-in-execution-speed-between-similar-functions-with-nested-loops/95710/3 "2023-03-08T04:53:48Z")

</div>

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)

```julia
; @ /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
}

```

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [March 8, 2023, 5:03am UTC](https://discourse.julialang.org/t/significant-difference-in-execution-speed-between-similar-functions-with-nested-loops/95710/4 "2023-03-08T05:03:15Z")

</div>

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
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)

```

---

<div class="post-metadata">

**Author:** ![aomidro](https://avatars.discourse-cdn.com/v4/letter/a/6a8cbe/32.png) [@aomidro](https://discourse.julialang.org/u/aomidro)\
**Post date:** [March 8, 2023, 5:26am UTC](https://discourse.julialang.org/t/significant-difference-in-execution-speed-between-similar-functions-with-nested-loops/95710/5 "2023-03-08T05:26:28Z")

</div>

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