# Benchmarking all() vs for-loop

**URL:** <https://discourse.julialang.org/t/benchmarking-all-vs-for-loop/84483>\
**Category:** General Usage\
**Tags:** question, benchmark\
**Created:** [July 19, 2022, 6:10pm UTC](https://discourse.julialang.org/t/benchmarking-all-vs-for-loop/84483 "2022-07-19T18:10:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![RobertGregg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robertgregg/32/22105_2.png) [@RobertGregg](https://discourse.julialang.org/u/RobertGregg)\
**Post date:** [July 19, 2022, 6:10pm UTC](https://discourse.julialang.org/t/benchmarking-all-vs-for-loop/84483/1 "2022-07-19T18:10:45Z")

</div>

Is it possible to get the same performance from the `all()` function compared to the for-loop equivalent?

```julia
v = rand(1:2,1_000_000)
idx = findall(isequal(1),v)

function f1(v,idx)
    @inbounds for i in idx
        if v[i] ≠ 1
            return false
        end
    end

    return true
end

function f2(v,idx)
    return @inbounds all(isequal(1), view(v,idx))
end

```

Benchmarking for me shows `f1()` taking ~211μs and `f2()` ~304μs, both with no allocations. Is the `all()` function just doing extra checks? This is mostly a curiosity, but I’d rather have a one-liner as opposed to a whole other function.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [July 19, 2022, 11:04pm UTC](https://discourse.julialang.org/t/benchmarking-all-vs-for-loop/84483/2 "2022-07-19T23:04:03Z")

</div>

I think you are paying the price of creating the `view`:

```julia
julia> function f1(v,idx)
           @inbounds for i in idx
               if v[i] ≠ 1
                   return false
               end
           end
           return true
       end
f1 (generic function with 1 method)

julia> f2(v,idx) = all(isequal(1), view(v,idx))
f2 (generic function with 1 method)

julia> function f3(v,idx)
           vv = @view(v[idx])
           for el in vv
               if el ≠ 1
                   return false
               end
           end
           return true
       end
f3 (generic function with 1 method)

julia> @btime f1($v, $idx)
  570.033 μs (0 allocations: 0 bytes)
true

julia> @btime f2($v, $idx)
  952.366 μs (0 allocations: 0 bytes)
true

julia> @btime f3($v, $idx)
  926.045 μs (0 allocations: 0 bytes)
true

```

which suggests this alternative:

```julia
julia> f4(v,idx) = all(@inbounds(v[i] == 1) for i in idx)
f4 (generic function with 1 method)

julia> f4(v,idx)
true

julia> @btime f4($v,$idx)
  567.193 μs (0 allocations: 0 bytes)
true

```

---

<div class="post-metadata">

**Author:** ![RobertGregg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robertgregg/32/22105_2.png) [@RobertGregg](https://discourse.julialang.org/u/RobertGregg)\
**Post date:** [July 20, 2022, 12:51am UTC](https://discourse.julialang.org/t/benchmarking-all-vs-for-loop/84483/3 "2022-07-20T00:51:23Z")

</div>

Gotta love generators 😄. I guess I was too focused on the `all(predicate, A)` syntax.

I also think of views as being virtually zero cost, but I guess that usually negligible amount can crop up in cases like this.

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [July 20, 2022, 12:53am UTC](https://discourse.julialang.org/t/benchmarking-all-vs-for-loop/84483/4 "2022-07-20T00:53:16Z")

</div>

> [@RobertGregg](#):
>
> I also think of views as being virtually zero cost, but I guess that usually negligible amount can crop up in cases like this.

I think that’s true for contiguous areas of memory, which is not the case with your `idx` collection of indices:

```julia
julia> @btime @view($v[$idx]);
  97.791 μs (0 allocations: 0 bytes)

julia> @btime @view($v[:]);
  1.458 ns (0 allocations: 0 bytes)

julia> @btime @view($v[begin:end÷2]);
  1.791 ns (0 allocations: 0 bytes)

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [July 20, 2022, 1:02am UTC](https://discourse.julialang.org/t/benchmarking-all-vs-for-loop/84483/5 "2022-07-20T01:02:00Z")

</div>

> [@RobertGregg](#):
>
> Gotta love generators

Yeah, the fact that that works well even when there is a non-true early element seems almost magical.

> [@RobertGregg](#):
>
> focused on the `all(predicate, A)` syntax

For what is worth, this also works:

```julia
julia> f5(v, idx) = all(isequal(1), @inbounds(v[i]) for i in idx)
f5 (generic function with 1 method)

julia> @btime f5($v,$idx)
  573.287 μs (0 allocations: 0 bytes)
true

```
