# Sum operations between arrays

**URL:** <https://discourse.julialang.org/t/sum-operations-between-arrays/37066>\
**Category:** Performance\
**Created:** [April 5, 2020, 4:11pm UTC](https://discourse.julialang.org/t/sum-operations-between-arrays/37066 "2020-04-05T16:11:27Z")\
**Posts on this page:** 2\
**Page:** 2

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [April 7, 2020, 4:28pm UTC](https://discourse.julialang.org/t/sum-operations-between-arrays/37066/21 "2020-04-07T16:28:15Z")

</div>

> [@Mason](#):
>
> but I suspect that the differences matter more on your system because the code is running so much faster on it and you have so many more threads available.

Could you try replacing `nchunks = Threads.nthreads()` with `nchunks = 2Threads.nthreads()` (or maybe just make it a function argument)? Two tasks per thread will balance the load better. This wasn’t worth the overhead on my system, but it may be worth it with yours.

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [April 7, 2020, 4:33pm UTC](https://discourse.julialang.org/t/sum-operations-between-arrays/37066/22 "2020-04-07T16:33:46Z")

</div>

Yeah, that gives it a slight edge over my implementation now:

```julia
julia> @benchmark tproduct_threads(x) setup=(x=rand(1000))
BenchmarkTools.Trial: 
  memory estimate: 7.65 MiB
  allocs estimate: 82
  --------------
  minimum time: 407.578 μs (0.00% GC)
  median time: 507.603 μs (0.00% GC)
  mean time: 791.596 μs (27.10% GC)
  maximum time: 2.968 ms (80.05% GC)
  --------------
  samples: 6270
  evals/sample: 1

julia> @benchmark tproduct_avx(x) setup=(x=rand(1000))
BenchmarkTools.Trial: 
  memory estimate: 7.66 MiB
  allocs estimate: 88
  --------------
  minimum time: 411.538 μs (0.00% GC)
  median time: 530.083 μs (0.00% GC)
  mean time: 692.385 μs (22.83% GC)
  maximum time: 2.735 ms (42.88% GC)
  --------------
  samples: 7170
  evals/sample: 1

```

(the runtimes on mine improved here from above because I now use `@avx` on the broadcasts and updated LoopVectorization.jl)

[Previous page](https://discourse.julialang.org/t/sum-operations-between-arrays/37066.md?page=1)
