LazyArrays.jl icon indicating copy to clipboard operation
LazyArrays.jl copied to clipboard

Logical Operators slower?

Open alfaromartino opened this issue 1 year ago • 5 comments

I was expecting that LazyArrays would outperform a generator when we use logical operators, but I see it's not. Is this expected? I include a MWE, as the page says that if we find slower code, it'd be helpful to indicate it.

This is faster

using Random, BenchmarkTools; Random.seed!(1234)
x = rand(100)

function foo(x)
    condition1(a)     = a > 0.25
    condition2(a)     = a < 0.75    
    all_conditions    = Iterators.map(a ->  condition1(a) && condition2(a) , x)    

    sum(all_conditions)
end

@btime foo($x)

relative to

using Random, BenchmarkTools; Random.seed!(1234)
x = rand(100)

function foo(x)
    condition1(a)     = a > 0.25
    condition2(a)     = a < 0.75
    all_conditions(a) = condition1(a) && condition2(a)
    
    sum(@~ all_conditions.(x))
end

@btime foo($x)

alfaromartino avatar Sep 30 '23 20:09 alfaromartino