Skip to content

extrema could be faster #44606

@LilithHafner

Description

@LilithHafner

This implementation seems to be slightly faster than extrema:

function _extrema(v)
    mn = mx = first(v)
    @inbounds for i in firstindex(v)+1:lastindex(v)
        vi = v[i]
        vi < mn && (mn = vi)
        mx < vi && (mx = vi)
    end
    mn, mx
end

@belapsed extrema(x) setup=(x=rand(Int, 10_000))
# 2.475222222222222e-6
@belapsed _extrema(x) setup=(x=rand(Int, 10_000))
# 2.0984e-6

@belapsed extrema(x) setup=(x=rand(Int, 10))
# 1.018937875751503e-8
@belapsed _extrema(x) setup=(x=rand(Int, 10))
# 7.78878878878879e-9

Originally posted by @LilithHafner in #44230 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions