Is there a pretty way to write this and have good performance? g1
produces runtime dispatch, g2
produces an allocation and g3
doesn’t look very “Julian” and is also unsafe.
function g1(x, y, w)
sum(i -> x[i] * y[i] * w[i], 1:length(x))
end
function g2(x, y, w)
sum(x .* y .* w)
end
function g3(x::AbstractArray{T}, y::AbstractArray{T}, w::AbstractArray{T}) where T
l = length(x)
r = T(0)
@inbounds for i in 1:l
r += x[i] * y[i] * w[i]
end
return r
end
function g4(x, y, w)
x .*= y
x .*= w
sum(x)
end
x = rand(3)
y = rand(3)
w = rand(3)
julia> @benchmark g1($x, $y, $w)
BenchmarkTools.Trial: 10000 samples with 999 evaluations.
Range (min … max): 9.969 ns … 46.624 ns ┊ GC (min … max): 0.00% … 0.00%
Time (median): 12.405 ns ┊ GC (median): 0.00%
Time (mean ± σ): 12.423 ns ± 0.565 ns ┊ GC (mean ± σ): 0.00% ± 0.00%
▃▁ ▂ ▁ ▄▇█▇ ▂
▃▃▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁██▁█▁█▄▆████▅▆▇▅▆▄▄▁▃▄▃▄▆▅▅▅▅▅▆▆▆▇ █
9.97 ns Histogram: log(frequency) by time 14.1 ns <
Memory estimate: 0 bytes, allocs estimate: 0.
julia> @benchmark g2($x, $y, $w)
BenchmarkTools.Trial: 10000 samples with 991 evaluations.
Range (min … max): 38.842 ns … 115.352 ns ┊ GC (min … max): 0.00% … 0.00%
Time (median): 49.113 ns ┊ GC (median): 0.00%
Time (mean ± σ): 48.779 ns ± 4.394 ns ┊ GC (mean ± σ): 0.00% ± 0.00%
▁▅█▇▃
▁▁▁▂▂▂▂▂▃▃▂▂▂▂▂▃▃▃▃▄▆█████▇▃▂▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁ ▂
38.8 ns Histogram: frequency by time 65.6 ns <
Memory estimate: 80 bytes, allocs estimate: 1.
julia> @benchmark g3($x, $y, $w)
BenchmarkTools.Trial: 10000 samples with 1000 evaluations.
Range (min … max): 3.135 ns … 22.953 ns ┊ GC (min … max): 0.00% … 0.00%
Time (median): 3.367 ns ┊ GC (median): 0.00%
Time (mean ± σ): 3.433 ns ± 0.426 ns ┊ GC (mean ± σ): 0.00% ± 0.00%
▂█
▅▃▁▃▁▁▃▅▄▅▅▆███▃▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▁▁▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▃▂▂▂▂▃▃▂▃ ▃
3.14 ns Histogram: frequency by time 4.25 ns <
Memory estimate: 0 bytes, allocs estimate: 0.
julia> @benchmark g4($x, $y, $w)
BenchmarkTools.Trial: 10000 samples with 999 evaluations.
Range (min … max): 8.153 ns … 35.191 ns ┊ GC (min … max): 0.00% … 0.00%
Time (median): 8.876 ns ┊ GC (median): 0.00%
Time (mean ± σ): 8.961 ns ± 0.570 ns ┊ GC (mean ± σ): 0.00% ± 0.00%
▄ ▁ ▅▆▇▇█▅▄▃▁ ▁▁ ▁▂▃▃▂▂▃ ▁▁▁ ▁ ▂
█▄▁▁▇█▄▆▁▄█▇▄▁███████████████████████████▆▆▅▅▅▅▅▄▅▅▄▄▅▄▅▆▆ █
8.15 ns Histogram: log(frequency) by time 10.5 ns <
Memory estimate: 0 bytes, allocs estimate: 0.