Why is minimum so much faster than argmin?

For fun

using LoopVectorization, BenchmarkTools
function findmin_turbo(x)
  indmin = 0
  minval = typemax(eltype(x))
  @turbo for i ∈ eachindex(x)
    newmin = x[i] < minval
    minval = newmin ? x[i] : minval
    indmin = newmin ?   i  : indmin
  end
  minval, indmin
end
y = rand(1000);
@btime findmin_turbo($y)
@btime findmin($y)
@btime minimum($y)
@btime argmin($y)

I get:

julia> y = rand(1000);

julia> @btime findmin_turbo($y)
  99.998 ns (0 allocations: 0 bytes)
(0.00021165397762579197, 79)

julia> @btime findmin($y)
  2.984 μs (0 allocations: 0 bytes)
(0.00021165397762579197, 79)

julia> @btime minimum($y)
  744.863 ns (0 allocations: 0 bytes)
0.00021165397762579197

julia> @btime argmin($y)
  2.954 μs (0 allocations: 0 bytes)
79

julia> versioninfo()
Julia Version 1.8.0-DEV.383
Commit eb83c4d25b* (2021-08-20 20:03 UTC)
Platform Info:
  OS: Linux (x86_64-redhat-linux)
  CPU: 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz

This requires LoopVectorization >= v0.12.66.

10 Likes