# Slow argmin?

**URL:** <https://discourse.julialang.org/t/slow-argmin/122556>\
**Category:** General Usage\
**Created:** [November 12, 2024, 5:31pm UTC](https://discourse.julialang.org/t/slow-argmin/122556 "2024-11-12T17:31:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)\
**Post date:** [November 12, 2024, 5:31pm UTC](https://discourse.julialang.org/t/slow-argmin/122556/1 "2024-11-12T17:31:33Z")

</div>

Hi,

I come across the following performance gap and I am not sure I understand why.

```julia
function my_min(a)
    val = a[begin]
    ind = 0
    @inbounds for i in eachindex(a)
        if a[i]>val
            val = a[i]
            ind = i
        end
    end
    ind
end

a = rand(1000)
julia> @btime argmin(a)
  4.554 μs (0 allocations: 0 bytes)
325

julia> @btime my_min(a)
  1.113 μs (0 allocations: 0 bytes)
325

```

---

<div class="post-metadata">

**Author:** ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)\
**Post date:** [November 12, 2024, 5:36pm UTC](https://discourse.julialang.org/t/slow-argmin/122556/2 "2024-11-12T17:36:46Z")

</div>

is it [Performance Difference between argmin and minimum · Issue #41963 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/41963) ?

---

<div class="post-metadata">

**Author:** ![roflmaostc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roflmaostc/32/30123_2.png) [@roflmaostc](https://discourse.julialang.org/u/roflmaostc)\
**Post date:** [November 12, 2024, 5:41pm UTC](https://discourse.julialang.org/t/slow-argmin/122556/3 "2024-11-12T17:41:17Z")

</div>

On recent nightly it seems to be improved compared to 1.11.1

EDIT: Apparently just benchmark noise, seems to be roughly the same.

```julia
julia> @benchmark argmin($a)
BenchmarkTools.Trial: 10000 samples with 10 evaluations.
 Range (min … max): 1.336 μs … 9.966 μs ┊ GC (min … max): 0.00% … 0.00%
 Time (median): 1.956 μs ┊ GC (median): 0.00%
 Time (mean ± σ): 2.066 μs ± 614.355 ns ┊ GC (mean ± σ): 0.00% ± 0.00%

  █ ▄▁ ▁ ▁ ▂ ▁ ▂ ▃ ▄                           
  █▇███▇███████▄▇██▄█▃█▇▆█▄▆▆▂█▂█▂▂█▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▁▂▁▁▁▇ ▄
  1.34 μs Histogram: frequency by time 3.94 μs <

 Memory estimate: 0 bytes, allocs estimate: 0.

julia> @benchmark my_min($a)
BenchmarkTools.Trial: 10000 samples with 84 evaluations.
 Range (min … max): 818.464 ns … 6.910 μs ┊ GC (min … max): 0.00% … 0.00%
 Time (median): 824.762 ns ┊ GC (median): 0.00%
 Time (mean ± σ): 876.617 ns ± 154.231 ns ┊ GC (mean ± σ): 0.00% ± 0.00%

  █▄▂▁▁▁ ▁
  ██████▇█▇█▆█▆█▅▆█▅█▄▆▇▅▇▄▄█▅▄▇▅▆█▆▃▇█▄▄█▅▅▇█▄▃▇█▄▃▃█▇▃▃▆█▅▅▃▄ █
  818 ns Histogram: log(frequency) by time 1.46 μs <

 Memory estimate: 0 bytes, allocs estimate: 0.

julia> versioninfo()
Julia Version 1.12.0-DEV.1613
Commit 366a38e6ed6 (2024-11-12 01:41 UTC)
Build Info:
  Official https://julialang.org release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 14 × Intel(R) Core(TM) Ultra 7 155U
  WORD_SIZE: 64
  LLVM: libLLVM-18.1.7 (ORCJIT, alderlake)
Threads: 14 default, 0 interactive, 14 GC (on 14 virtual cores)
Environment:
  JULIA_MAX_NUM_PRECOMPILE_FILES = 100
  JULIA_NUM_THREADS = 14
  JULIA_PKG_PRESERVE_TIERED_INSTALLED = true

```

Vs

```julia
julia> @benchmark argmin($a) evals=100000
BenchmarkTools.Trial: 32 samples with 100000 evaluations.
 Range (min … max): 1.118 μs … 3.082 μs ┊ GC (min … max): 0.00% … 0.00%
 Time (median): 1.164 μs ┊ GC (median): 0.00%
 Time (mean ± σ): 1.559 μs ± 691.308 ns ┊ GC (mean ± σ): 0.00% ± 0.00%

  █                                                            
  ██▄▃▄▃▁▁▁▁▁▁▁▁▁▃▁▁▁▁▁▁▁▃▁▁▁▁▁▃▁▁▁▁▁▁▃▁▁▁▁▁▁▁▁▁▁▁▃▁▁▁▁▁▁▁▃▁▆ ▁
  1.12 μs Histogram: frequency by time 3.08 μs <

 Memory estimate: 0 bytes, allocs estimate: 0.

julia> @benchmark my_min($a) evals=100000
BenchmarkTools.Trial: 42 samples with 100000 evaluations.
 Range (min … max): 823.790 ns … 2.346 μs ┊ GC (min … max): 0.00% … 0.00%
 Time (median): 828.805 ns ┊ GC (median): 0.00%
 Time (mean ± σ): 1.194 μs ± 602.326 ns ┊ GC (mean ± σ): 0.00% ± 0.00%

  █ ▁  
  █▁▁▁▁▁▁▇▁▁▁▁▁▁▁▁▅▁▁▁▁▁▅▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▅▅▅▁▁▁▁▁▁▇▁▅█ ▁
  824 ns Histogram: log(frequency) by time 2.35 μs <

 Memory estimate: 0 bytes, allocs estimate: 0.

julia> versioninfo()
Julia Version 1.11.1
Commit 8f5b7ca12ad (2024-10-16 10:53 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 14 × Intel(R) Core(TM) Ultra 7 155U
  WORD_SIZE: 64
  LLVM: libLLVM-16.0.6 (ORCJIT, alderlake)
Threads: 14 default, 0 interactive, 7 GC (on 14 virtual cores)
Environment:
  JULIA_MAX_NUM_PRECOMPILE_FILES = 100
  JULIA_NUM_THREADS = 14
  JULIA_PKG_PRESERVE_TIERED_INSTALLED = true

```

---

<div class="post-metadata">

**Author:** ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)\
**Post date:** [November 12, 2024, 5:46pm UTC](https://discourse.julialang.org/t/slow-argmin/122556/4 "2024-11-12T17:46:26Z")

</div>

probably
