Skip to content

hypot performance poor for more than 3 arguments #44336

@sethaxen

Description

@sethaxen

When 4 or more arguments are pass to hypot, its performance suddenly becomes very poor.

julia> using BenchmarkTools

julia> f(x) = hypot(x...)
f (generic function with 1 method)

julia> x = (1.0, 2.0, 3.0)
(1.0, 2.0, 3.0)

julia> @btime f($x)   # fine
  11.415 ns (0 allocations: 0 bytes)
3.741657386773941

julia> x = (1.0, 2.0, 3.0, 4.0)
(1.0, 2.0, 3.0, 4.0)

julia> @btime f($x)  # so slow!!
  708.965 ns (13 allocations: 336 bytes)
5.477225575051661

Calling hypot pairwise is much faster here

julia> myhypot(x...) = reduce(hypot, x)
myhypot (generic function with 1 method)

julia> g(x) = myhypot(x...)
g (generic function with 1 method)

julia> @btime g($x)
  41.038 ns (0 allocations: 0 bytes)
5.477225575051661

The trend continues even for large tuples:

julia> x = (randn(100)...,);

julia> @btime f($x)
  23.303 μs (623 allocations: 19.14 KiB)
9.54225022090292

julia> @btime g($x)
  9.713 μs (207 allocations: 7.14 KiB)
9.54225022090292

Is there a good reason we shouldn't adopt this default for hypot(x1, x2, x3, x...)?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions