# Why is this simple function twice as slow as its Python version

**URL:** https://discourse.julialang.org/t/why-is-this-simple-function-twice-as-slow-as-its-python-version/59004
**Category:** Performance
**Tags:** question
**Created:** [April 10, 2021, 6:58pm UTC](https://discourse.julialang.org/t/why-is-this-simple-function-twice-as-slow-as-its-python-version/59004 "2021-04-10T18:58:14Z")
**Posts on this page:** 1
**Showing post:** 95

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [April 12, 2021, 1:36pm UTC](https://discourse.julialang.org/t/why-is-this-simple-function-twice-as-slow-as-its-python-version/59004/95 "2021-04-12T13:36:32Z")

</div>

May I summarize what we learnt here?

1. The original post had python doing `a*b` which is not a matrix multiplication. Thus the comparison was not correct. That was changed to `a@b`, and the difference in performance reported is much smaller (30%), not “twice”.
2. The remaining difference in performance is somewhat system dependent. If I copy/paste now the original codes I get, in my machine, the same time for both of them (`~950 ms`). Yet the benchmarks oscilate a bit, because they are calling BLAS with multi-threading on the background, and probably other programs can compete for the processor usage. There may be an issue concerning the number of threads launched by the BLAS routine.
3. That, considering the fact that in Python the line `tmp2[...] = t@tmp1` is not allocating a new array, while the line `tmp2[...] = t*tmp1` is allocating a new array in Julia.
4. Solving that specific allocation in Julia requires a more verbose syntax (`mul!(@view(tmp2[...]),t,tmp1)`. It might improve slightly the performance (10% maybe), but the timings vary because of the same reasons above.
5. Avoding other allocations can readily make the Julia code run 2x faster than the original one. That can probably be done with Python as well.
6. More advanced modifications and 32 bit representation of the matrices can make the code 50x faster than the original one (Elrod `batch` version), but that is advanced indeed.

Finally, I congratulate all for the very pleasant and civilized conversation! 🙂

---

_[View the full topic](https://discourse.julialang.org/t/why-is-this-simple-function-twice-as-slow-as-its-python-version/59004)._
