# Reduce vs splat with intersect() function performance

**URL:** <https://discourse.julialang.org/t/reduce-vs-splat-with-intersect-function-performance/51583>\
**Category:** Performance\
**Tags:** question\
**Created:** [December 10, 2020, 4:09am UTC](https://discourse.julialang.org/t/reduce-vs-splat-with-intersect-function-performance/51583 "2020-12-10T04:09:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rapasite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rapasite/32/3719_2.png) [@rapasite](https://discourse.julialang.org/u/rapasite)\
**Post date:** [December 10, 2020, 4:09am UTC](https://discourse.julialang.org/t/reduce-vs-splat-with-intersect-function-performance/51583/1 "2020-12-10T04:09:48Z")

</div>

Hi.  
Just trying to understand why the first `sum` is faster than the second.

```julia
using BenchmarkTools
const data=raw"C:\text.txt" |> read |> String |> x->split(x,r"\r\n\r\n|\n\n") .|> y->split(y,r"\r\n|\r|\n")

@btime sum(length,data .|> x->∩(x...))#Use of splat

@btime sum(length,data .|> x->reduce(∩, x))#use of reduce

```

```julia
1.620 ms (14963 allocations: 1.24 MiB)
2.253 ms (22777 allocations: 1.76 MiB)

```

Thanks

---

<div class="post-metadata">

**Author:** ![FPGro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fpgro/32/20822_2.png) [@FPGro](https://discourse.julialang.org/u/FPGro)\
**Post date:** [December 11, 2020, 1:08am UTC](https://discourse.julialang.org/t/reduce-vs-splat-with-intersect-function-performance/51583/2 "2020-12-11T01:08:20Z")

</div>

You may look at the implementation of `intersect` here:

[https://github.com/JuliaLang/julia/blob/788b2c77c10c2160f4794a4d4b6b81a95a90940c/base/abstractset.jl#L120](https://github.com/JuliaLang/julia/blob/788b2c77c10c2160f4794a4d4b6b81a95a90940c/base/abstractset.jl#L120)

`reduce(∩, [a, b, c, d]) == ∩(a, ∩(b, ∩(c, d)))`

And the 2-argument intersect allocates a new destination each time, whereas the arbitrary elements intersect reuses the destination of 1 `intersect` call and then calls `intersect!` on that.
