The result changes when data
is not Bool
. I was not sure if that was intentional that data
is a Bool
.
Code
using Random
using BenchmarkTools
using LinearAlgebra
Random.seed!(0)
N = 1_000_000
data = rand(Bool, N)
mask = rand((true, false), N)
f_viewalloc(data, mask) = @views sum(data[mask])
@show @btime f_viewalloc($data, $mask) # 4.859 ms (2 allocs: 3.8 MiB)
f_viewalloc1(data, mask) = sum(last, Iterators.filter(first, zip(mask,data)))
@show @btime f_viewalloc1($data, $mask) # 3.659 ms (0 allocs: 0 bytes)
f_viewalloc3(data, mask) = mapreduce(*, +, data, mask)
@show @btime f_viewalloc3($data, $mask) # 153 ÎĽs (2 allocs: 977 KiB)
f_generator_fast(data, mask) = sum(v*mask[i] for (i,v) in pairs(data))
@show @btime f_generator_fast($data, $mask) # 129 ÎĽs (0 allocs: 0 bytes)
f_dot_product(data, mask) = sum(data .* mask)
@show @btime f_dot_product($data, $mask) # 106 ÎĽs (4 allocs: 126 KiB)
f_dot_product2(data, mask) = data' * mask
@show @btime f_dot_product2($data, $mask) # 771 ÎĽs (0 allocs: 0 bytes)
f_dot_product3(data, mask) = dot(data, mask)
@show @btime f_dot_product3($data, $mask) # 771 ÎĽs (0 allocs: 0 bytes)
julia> include("/tmp/benchmark.jl") # data is Float64
4.126 ms (2 allocations: 3.81 MiB)
#= /tmp/benchmark.jl:10 =# @btime(f_viewalloc($(Expr(:$, :data)), $(Expr(:$, :mask)))) = 249765.49111151227
3.115 ms (0 allocations: 0 bytes)
#= /tmp/benchmark.jl:13 =# @btime(f_viewalloc1($(Expr(:$, :data)), $(Expr(:$, :mask)))) = 249765.49111151227
1.390 ms (6 allocations: 7.63 MiB)
#= /tmp/benchmark.jl:16 =# @btime(f_viewalloc3($(Expr(:$, :data)), $(Expr(:$, :mask)))) = 249765.49111151224
3.293 ms (0 allocations: 0 bytes)
#= /tmp/benchmark.jl:19 =# @btime(f_generator_fast($(Expr(:$, :data)), $(Expr(:$, :mask)))) = 249765.49111151227
1.398 ms (2 allocations: 7.63 MiB)
#= /tmp/benchmark.jl:22 =# @btime(f_dot_product($(Expr(:$, :data)), $(Expr(:$, :mask)))) = 249765.49111151224
3.170 ms (0 allocations: 0 bytes)
#= /tmp/benchmark.jl:25 =# @btime(f_dot_product2($(Expr(:$, :data)), $(Expr(:$, :mask)))) = 249765.49111151227
3.094 ms (0 allocations: 0 bytes)
#= /tmp/benchmark.jl:28 =# @btime(f_dot_product3($(Expr(:$, :data)), $(Expr(:$, :mask)))) = 249765.49111151227
249765.49111151227
julia> include("/tmp/benchmark.jl") # data is bool
3.876 ms (2 allocations: 3.81 MiB)
#= /tmp/benchmark.jl:10 =# @btime(f_viewalloc($(Expr(:$, :data)), $(Expr(:$, :mask)))) = 249502
2.910 ms (0 allocations: 0 bytes)
#= /tmp/benchmark.jl:13 =# @btime(f_viewalloc1($(Expr(:$, :data)), $(Expr(:$, :mask)))) = 249502
137.107 ÎĽs (2 allocations: 976.67 KiB)
#= /tmp/benchmark.jl:16 =# @btime(f_viewalloc3($(Expr(:$, :data)), $(Expr(:$, :mask)))) = 249502
104.426 ÎĽs (0 allocations: 0 bytes)
#= /tmp/benchmark.jl:19 =# @btime(f_generator_fast($(Expr(:$, :data)), $(Expr(:$, :mask)))) = 249502
84.967 ÎĽs (4 allocations: 126.39 KiB)
#= /tmp/benchmark.jl:22 =# @btime(f_dot_product($(Expr(:$, :data)), $(Expr(:$, :mask)))) = 249502
652.136 ÎĽs (0 allocations: 0 bytes)
#= /tmp/benchmark.jl:25 =# @btime(f_dot_product2($(Expr(:$, :data)), $(Expr(:$, :mask)))) = 249502
636.865 ÎĽs (0 allocations: 0 bytes)
#= /tmp/benchmark.jl:28 =# @btime(f_dot_product3($(Expr(:$, :data)), $(Expr(:$, :mask)))) = 249502
249502
``