# Implement an efficient dot-product-like algorithm for two or more vectors?

**URL:** <https://discourse.julialang.org/t/implement-an-efficient-dot-product-like-algorithm-for-two-or-more-vectors/19623>\
**Category:** General Usage\
**Created:** [January 14, 2019, 3:59pm UTC](https://discourse.julialang.org/t/implement-an-efficient-dot-product-like-algorithm-for-two-or-more-vectors/19623 "2019-01-14T15:59:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [January 14, 2019, 3:59pm UTC](https://discourse.julialang.org/t/implement-an-efficient-dot-product-like-algorithm-for-two-or-more-vectors/19623/1 "2019-01-14T15:59:48Z")

</div>

I’m trying to fill a vector `A` of length `n` with the dot-product of two vectors `x` and `y` like this

```julia
A = fill(sum(x[i]*y[i] for i in eachindex(x)), n)

```

For three vectors `x`, `y`, and `z`, this can be written as

```julia
B = fill(sum(x[i]*y[i]*z[i] for i in eachindex(x)), n)

```

I want to generalize this for arbitrary number of vectors (two or more) using a function, e.g., without allocating extra temporary vectors. Is it easy to implement this or should I think of another way to do it?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 14, 2019, 4:04pm UTC](https://discourse.julialang.org/t/implement-an-efficient-dot-product-like-algorithm-for-two-or-more-vectors/19623/2 "2019-01-14T16:04:29Z")

</div>

Maybe `fillsumprod(x, y...) = fill(sum(prod(z) for z in zip(x,y...)), length(x))`. On my machine, this has performance within 1% of the manual 2-argument version you wrote above.

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [January 14, 2019, 4:46pm UTC](https://discourse.julialang.org/t/implement-an-efficient-dot-product-like-algorithm-for-two-or-more-vectors/19623/3 "2019-01-14T16:46:44Z")

</div>

Thank you very much, this is elegant indeed. But, I observe about 17% and 37% degradation on my machine compared to explicit loops for the two vectors and three vectors, respectively. Actually, this is part of a benchmark for Julia against Python+Numpy in which Julia already beats Numpy by a good margin using the explicit loops as above. I was trying to show my audience how Julia is both fast and elegant at the same time. I wonder if this can be made a bit faster?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 14, 2019, 4:54pm UTC](https://discourse.julialang.org/t/implement-an-efficient-dot-product-like-algorithm-for-two-or-more-vectors/19623/4 "2019-01-14T16:54:40Z")

</div>

Make sure you benchmark using BenchmarkTools with `@btime fillsumprod($x,$y)` … I used `rand(100)` vectors.

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [January 14, 2019, 5:02pm UTC](https://discourse.julialang.org/t/implement-an-efficient-dot-product-like-algorithm-for-two-or-more-vectors/19623/5 "2019-01-14T17:02:06Z")

</div>

Of course, I use `@btime fillsumprod($x, $y);` but my vectors are much bigger, 5e6 elements.
