Reduce vs splat with intersect() function performance

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

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
1.620 ms (14963 allocations: 1.24 MiB)
2.253 ms (22777 allocations: 1.76 MiB)

Thanks

You may look at the implementation of intersect here:

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.

3 Likes