JuliaFolds / Transducers.jl

Efficient transducers for Julia
https://juliafolds.github.io/Transducers.jl/dev/
MIT License
432 stars 24 forks source link

Performance issue in `zip` without `init`; e.g., foldxl(+, MapSplat(==), zip(x, y)) #437

Open tkf opened 3 years ago

tkf commented 3 years ago
using Transducers
using BenchmarkTools

x = randn(10240);
y = randn(10240);
function f2(x, y)
    total = 0
    @inbounds for i in 1:length(x)
        total += x[i] == y[i]
    end
    return total
end

f3(x, y) = foldxl(+, MapSplat(==), zip(x, y); simd = true)
f4(x, y) = foldxl(+, MapSplat(==), zip(x, y); init = 0, simd = true)

@btime f2(x, y)
@btime f3(x, y)
@btime f4(x, y)

prints

  2.694 μs (0 allocations: 0 bytes)
  44.666 μs (2 allocations: 48 bytes)
  3.325 μs (2 allocations: 48 bytes)