# Base.product repeat 1:2 5 times

**URL:** https://discourse.julialang.org/t/base-product-repeat-1-2-5-times/16642
**Category:** General Usage
**Created:** [October 22, 2018, 6:02pm UTC](https://discourse.julialang.org/t/base-product-repeat-1-2-5-times/16642 "2018-10-22T18:02:56Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Paul\_McVay](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_mcvay/32/4307_2.png) [@Paul\_McVay](https://discourse.julialang.org/u/Paul_McVay)
#### Post date: [October 22, 2018, 6:02pm UTC](https://discourse.julialang.org/t/base-product-repeat-1-2-5-times/16642/1 "2018-10-22T18:02:56Z")

</div>

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

```julia
Base.product(1:2, 1:2, 1:2, 1:2, 1:2)

```

Be able to type

```julia
Base.product(repeat(1:2, 5))

```

---

<div class="post-metadata">

### Author: ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)
#### Post date: [October 22, 2018, 6:17pm UTC](https://discourse.julialang.org/t/base-product-repeat-1-2-5-times/16642/2 "2018-10-22T18:17:06Z")

</div>

You could (but shouldn’t) write

```julia
Base.product(fill(1:2, 5)...)

```

This will be much slower as it creates an intermediate array:

```julia
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.

---

<div class="post-metadata">

### Author: ![favba](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/favba/32/2735_2.png) [@favba](https://discourse.julialang.org/u/favba)
#### Post date: [October 22, 2018, 6:18pm UTC](https://discourse.julialang.org/t/base-product-repeat-1-2-5-times/16642/3 "2018-10-22T18:18:18Z")

</div>

This is what I came up with

```julia
Base.product(ntuple(i->1:2,Val{5}())...)

```

```julia
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))

```

---

<div class="post-metadata">

### Author: ![Paul\_McVay](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_mcvay/32/4307_2.png) [@Paul\_McVay](https://discourse.julialang.org/u/Paul_McVay)
#### Post date: [October 22, 2018, 6:34pm UTC](https://discourse.julialang.org/t/base-product-repeat-1-2-5-times/16642/4 "2018-10-22T18:34:41Z")

</div>

Just out of curiosity (I’m new to julia), what do you use `Val{5}()`? I get the same performance with just `5`

---

<div class="post-metadata">

### Author: ![favba](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/favba/32/2735_2.png) [@favba](https://discourse.julialang.org/u/favba)
#### Post date: [October 22, 2018, 6:56pm UTC](https://discourse.julialang.org/t/base-product-repeat-1-2-5-times/16642/5 "2018-10-22T18:56:47Z")

</div>

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
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

```
