-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Closed
Description
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.477225575051661Calling 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.477225575051661The 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.54225022090292Is there a good reason we shouldn't adopt this default for hypot(x1, x2, x3, x...)?
Metadata
Metadata
Assignees
Labels
No labels