# Fastest way to multiply a constant sparse matrix to a vector

**URL:** <https://discourse.julialang.org/t/fastest-way-to-multiply-a-constant-sparse-matrix-to-a-vector/16458>\
**Category:** Performance\
**Created:** [October 17, 2018, 6:54pm UTC](https://discourse.julialang.org/t/fastest-way-to-multiply-a-constant-sparse-matrix-to-a-vector/16458 "2018-10-17T18:54:23Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![briochemc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/briochemc/32/4209_2.png) [@briochemc](https://discourse.julialang.org/u/briochemc)\
**Post date:** [October 17, 2018, 6:54pm UTC](https://discourse.julialang.org/t/fastest-way-to-multiply-a-constant-sparse-matrix-to-a-vector/16458/1 "2018-10-17T18:54:23Z")

</div>

One answer to [this question](https://discourse.julialang.org/t/fastest-way-of-doing-constant-sparse-svector-multiplication/16365) about multiplying a constant sparse matrix to a _static_ vector made me wonder:

Is there a more performant way of multiplying a constant sparse matrix `A` to a _non-static_ vector `x` than doing `A * x`?

(e.g., for a matrix and vector of large sizes, which do not work well with StaticArrays)

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [October 17, 2018, 6:55pm UTC](https://discourse.julialang.org/t/fastest-way-to-multiply-a-constant-sparse-matrix-to-a-vector/16458/2 "2018-10-17T18:55:56Z")

</div>

> [@briochemc](#):
>
> Is there a more performant way of multiplying a constant sparse matrix `A` to a _non-static_ vector `x` than doing `A * x` ?

`mul!(y,A,x)`

---

<div class="post-metadata">

**Author:** ![briochemc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/briochemc/32/4209_2.png) [@briochemc](https://discourse.julialang.org/u/briochemc)\
**Post date:** [October 17, 2018, 9:55pm UTC](https://discourse.julialang.org/t/fastest-way-to-multiply-a-constant-sparse-matrix-to-a-vector/16458/3 "2018-10-17T21:55:46Z")

</div>

> [@ChrisRackauckas](#):
>
> `mul!(y,A,x)`

But this is just a memory thing, right? Is it different (in speed) to doing `y .= A * x`?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [October 17, 2018, 10:01pm UTC](https://discourse.julialang.org/t/fastest-way-to-multiply-a-constant-sparse-matrix-to-a-vector/16458/4 "2018-10-17T22:01:39Z")

</div>

> [@briochemc](#):
>
> Is it different (in speed) to doing `y .= A * x` ?

Yes, `A*x` allocates `y` and then does `mul!`. So it’s faster than using `*`, but only by the amount it takes to allocate `y`.

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [October 17, 2018, 10:10pm UTC](https://discourse.julialang.org/t/fastest-way-to-multiply-a-constant-sparse-matrix-to-a-vector/16458/5 "2018-10-17T22:10:12Z")

</div>

Then there is the copying from the result of `A * x` to `y`, as opposed to filling `y` directly which `mul!` does.

---

<div class="post-metadata">

**Author:** ![briochemc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/briochemc/32/4209_2.png) [@briochemc](https://discourse.julialang.org/u/briochemc)\
**Post date:** [October 17, 2018, 11:24pm UTC](https://discourse.julialang.org/t/fastest-way-to-multiply-a-constant-sparse-matrix-to-a-vector/16458/6 "2018-10-17T23:24:06Z")

</div>

OK I see thanks!

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [October 17, 2018, 11:53pm UTC](https://discourse.julialang.org/t/fastest-way-to-multiply-a-constant-sparse-matrix-to-a-vector/16458/7 "2018-10-17T23:53:59Z")

</div>

MKL has some new interesting stuff with the Inspector-executor API ([Intel | Data Center Solutions, IoT, and PC Innovation](https://software.intel.com/en-us/mkl-developer-reference-c-inspector-executor-sparse-blas-routines)). You give an estimate on how many times you will do an operation and it optimizes the operation for the given matrix.

---

<div class="post-metadata">

**Author:** ![Juser](https://avatars.discourse-cdn.com/v4/letter/j/34f0e0/32.png) [@Juser](https://discourse.julialang.org/u/Juser)\
**Post date:** [October 22, 2018, 2:44pm UTC](https://discourse.julialang.org/t/fastest-way-to-multiply-a-constant-sparse-matrix-to-a-vector/16458/8 "2018-10-22T14:44:41Z")

</div>

> [@ChrisRackauckas](#):
>
> Yes, `A*x` allocates `y` and then does `mul!` . So it’s faster than using `*` , but only by the amount it takes to allocate `y` .

Out of curiosity, is there any reason that the compiler doesn’t just read `y .= A * x` to mean `mul!(y,A,x)`? Is there some case in which the user doesn’t want the same thing to happen? (For the elements of y to reflect `A*x`.)
