Slow argmin?

Hi,

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

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
2 Likes

is it Performance Difference between argmin and minimum ยท Issue #41963 ยท JuliaLang/julia ยท GitHub ?

2 Likes

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

1 Like

probably