For the Base.product function, is there a simple way to repeat the input array 1:2 5 times? So for example, instead of having to type
Base.product(1:2, 1:2, 1:2, 1:2, 1:2)
Be able to type
Base.product(repeat(1:2, 5))
For the Base.product function, is there a simple way to repeat the input array 1:2 5 times? So for example, instead of having to type
Base.product(1:2, 1:2, 1:2, 1:2, 1:2)
Be able to type
Base.product(repeat(1:2, 5))
You could (but shouldn’t) write
Base.product(fill(1:2, 5)...)
This will be much slower as it creates an intermediate array:
julia> @btime Base.product(1:2, 1:2, 1:2, 1:2, 1:2)
2.133 ns (0 allocations: 0 bytes)
Base.Iterators.ProductIterator{NTuple{5,UnitRange{Int64}}}((1:2, 1:2, 1:2, 1:2, 1:2))
julia> @btime Base.product(fill(1:2, 5)...)
521.232 ns (8 allocations: 512 bytes)
Base.Iterators.ProductIterator{NTuple{5,UnitRange{Int64}}}((1:2, 1:2, 1:2, 1:2, 1:2))
If you really want to write less and keep performance you could use a macro I guess.
This is what I came up with
Base.product(ntuple(i->1:2,Val{5}())...)
julia> using BenchmarkTools
julia> @btime Base.product(1:2, 1:2, 1:2, 1:2, 1:2)
2.145 ns (0 allocations: 0 bytes)
Base.Iterators.ProductIterator{NTuple{5,UnitRange{Int64}}}((1:2, 1:2, 1:2, 1:2, 1:2))
julia> @btime Base.product(fill(1:2, 5)...)
580.244 ns (8 allocations: 512 bytes)
Base.Iterators.ProductIterator{NTuple{5,UnitRange{Int64}}}((1:2, 1:2, 1:2, 1:2, 1:2))
julia> @btime Base.product(ntuple(i->1:2,Val{5}())...)
2.144 ns (0 allocations: 0 bytes)
Base.Iterators.ProductIterator{NTuple{5,UnitRange{Int64}}}((1:2, 1:2, 1:2, 1:2, 1:2))
Just out of curiosity (I’m new to julia), what do you use Val{5}()
? I get the same performance with just 5
That is to guarantee that the length of the tuple will be known at compile time.
With constant propagation it might be that using 5
directly will still work as performant as with using Val{5}()
, but it is not guaranteed.
For ex:
julia> using BenchmarkTools
julia> function g(n)
l = Base.product(ntuple(i->1:2,n)...)
x = 0
for i in l
x+=1
end
return x
end
g (generic function with 1 method)
julia> @btime g(5)
2.611 μs (72 allocations: 7.44 KiB)
32
julia> @btime g(Val{5}())
27.610 ns (0 allocations: 0 bytes)
32