# Sparse matrix-vector product: much more slow than Matlab

**URL:** https://discourse.julialang.org/t/sparse-matrix-vector-product-much-more-slow-than-matlab/7840
**Category:** Performance
**Tags:** matlab, optimization
**Created:** [December 18, 2017, 9:14pm UTC](https://discourse.julialang.org/t/sparse-matrix-vector-product-much-more-slow-than-matlab/7840 "2017-12-18T21:14:14Z")
**Posts on this page:** 1
**Showing post:** 6

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [December 19, 2017, 3:29am UTC](https://discourse.julialang.org/t/sparse-matrix-vector-product-much-more-slow-than-matlab/7840/6 "2017-12-19T03:29:05Z")

</div>

1. Please quote your code so that it’s easy to read: [PSA: how to quote code with backticks](https://discourse.julialang.org/t/psa-how-to-quote-code-with-backticks/7530)
2. semicolons at the end of each line are not necessary
3. When benchmarking code, you will _not_ get accurate results when timing in global scope. That’s why `@StefanKarpinski` asked you earlier. Instead, put the code you’re timing in a function. Running your code in a function, I see `3.699408 seconds (41.60 k allocations: 3.787 GiB, 5.39% gc time)` which is already quite close to what you reported MATLAB as giving.

You are seeing a lot of allocations because your code really does allocate a lot of memory. In particular, you are constructing new matrices to hold a lot of intermediate quantities. Modifying your code to pre-allocate those matrices may help a lot. One easy improvement is to broadcast the first line in your loop to avoid allocating a matrix for ` (sparseR + reshape(q' * sparseS, 199, 199))` and then another one for ` 0.5 * 0.05 * (sparseR + reshape(q' * sparseS, 199, 199))`:

```julia
tmp = 0.5 .* 0.05 .* (sparseR .+ reshape(q' * sparseS, 199, 199))

```

---

_[View the full topic](https://discourse.julialang.org/t/sparse-matrix-vector-product-much-more-slow-than-matlab/7840)._
