How to sum elements raised to a power

You can have a faster sum (not as safe as the default sum, which takes some care to not overflow things), using a parallel sum with @tturbo:

julia> function mysum(f,x)
           s = zero(eltype(f(x[begin])))
           @tturbo for i in eachindex(x)
               s += f(x[i])
           end
           return s
       end
mysum (generic function with 2 methods)

julia> x = rand(1000,1000);

julia> @btime mysum(abs2,$x) # 4 threads (julia -t4)
  83.500 μs (0 allocations: 0 bytes)
333115.0216837497

julia> #vs
       @btime sum(abs2,$x)
  327.982 μs (0 allocations: 0 bytes)
333115.0216837495

If that is within a peformance critical part of the code and the numbers are well behaved, it is a good alternative.

2 Likes