# Multithreading on Cartesian products

**URL:** <https://discourse.julialang.org/t/multithreading-on-cartesian-products/54009>\
**Category:** General Usage\
**Tags:** multithreading\
**Created:** [January 26, 2021, 7:57pm UTC](https://discourse.julialang.org/t/multithreading-on-cartesian-products/54009 "2021-01-26T19:57:12Z")\
**Posts on this page:** 3\
**Page:** 2

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [January 28, 2021, 6:04pm UTC](https://discourse.julialang.org/t/multithreading-on-cartesian-products/54009/21 "2021-01-28T18:04:23Z")

</div>

There’s still some advantage:

```julia
julia> function f()
           v = zeros(40^5);
           map!(i -> i, v, eachindex(v))
       end
f (generic function with 1 method)

julia> function g()
           v = Array{Float64}(undef, 40^5);
           map!(i -> i, v, eachindex(v))
       end
g (generic function with 1 method)

julia> @btime f();
  206.253 ms (2 allocations: 781.25 MiB)

julia> @btime g();
  146.827 ms (2 allocations: 781.25 MiB)

```

---

<div class="post-metadata">

**Author:** ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)\
**Post date:** [January 28, 2021, 6:59pm UTC](https://discourse.julialang.org/t/multithreading-on-cartesian-products/54009/22 "2021-01-28T18:59:08Z")

</div>

Yes indeed. I was reacting to the _considerably_ term.

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [January 28, 2021, 9:43pm UTC](https://discourse.julialang.org/t/multithreading-on-cartesian-products/54009/23 "2021-01-28T21:43:20Z")

</div>

In my machine (with `julia` started with four threads), I get a slight speedup

```julia
julia> @btime x = testFLoops(n);
  532.133 ms (291 allocations: 781.27 MiB)

julia> @btime y = testFLoops(n,SequentialEx());
  702.121 ms (23 allocations: 781.25 MiB)

```

But yeah, that’s not really a nice speedup. If I extract out the allocations, I get a bit more speedup (though still not great):

```julia
julia> begin
       function testFLoops!(x, focals, ex=ThreadedEx())
           @floop ex for (i,focal) in enumerate(focals)
               x[i] = floor(rand() + Threads.threadid())
           end
           return x
       end

       n = 40
       myarr = [1:n for i in 1:5]
       numElements = prod(length(i) for i in myarr)
       x = zeros(numElements)
       focals = Iterators.product((1:length(j) for j in myarr)...)

       @btime testFLoops!($x, $focals)
       @btime testFLoops!($x, $focals, SequentialEx())
       end;
  332.903 ms (273 allocations: 21.50 KiB)
  508.605 ms (5 allocations: 240 bytes)

```

[Previous page](https://discourse.julialang.org/t/multithreading-on-cartesian-products/54009.md?page=1)
