-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Closed as duplicate of#47216
Labels
foldsum, maximum, reduce, foldl, etc.sum, maximum, reduce, foldl, etc.performanceMust go fasterMust go faster
Description
reduce
misses the opportunity to re-associate operations when given NTuple
arguments. This has performance impacts on notable functions such as sum(::NTuple)
. One can use @fastmath
to permit re-association in some cases, but I'm never a fan of suggesting @fastmath
to people.
julia> using BenchmarkTools
julia> x = ntuple(_->randn(), 16);
julia> @btime sum($x)
5.700 ns (0 allocations: 0 bytes)
-2.8324452894507908
julia> @btime @fastmath(reduce(+,$x))
3.100 ns (0 allocations: 0 bytes)
-2.8324452894507903
You can see the lack of vectorization by comparing
code_native(sum,Tuple{NTuple{16,Float64}};debuginfo=:none)
to the SIMDing version
code_native(x->@fastmath(reduce(+,x)),Tuple{NTuple{16,Float64}};debuginfo=:none)
.
Methods like sum(::Vector)
already do re-associate (with performance and accuracy benefits).
StefanKarpinski and uniment
Metadata
Metadata
Assignees
Labels
foldsum, maximum, reduce, foldl, etc.sum, maximum, reduce, foldl, etc.performanceMust go fasterMust go faster