# Benchmarking Julia vs NumPy

**URL:** https://discourse.julialang.org/t/benchmarking-julia-vs-numpy/36695
**Category:** New to Julia
**Tags:** benchmark
**Created:** [March 29, 2020, 8:04pm UTC](https://discourse.julialang.org/t/benchmarking-julia-vs-numpy/36695 "2020-03-29T20:04:55Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Tasty\_Minerals](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tasty_minerals/32/13698_2.png) [@Tasty\_Minerals](https://discourse.julialang.org/u/Tasty_Minerals)
#### Post date: [March 29, 2020, 8:04pm UTC](https://discourse.julialang.org/t/benchmarking-julia-vs-numpy/36695/1 "2020-03-29T20:04:55Z")

</div>

Hello, I decided to make couple of benchmarks against NumPy out of curiosity.  
Since I don’t know Julia it would be nice if someone could take a second look because based on what I measured it doesn’t look like Julia is faster especially in matrix multiplication and norm.

The results are here [https://gist.github.com/tastyminerals/f6764ff472c8e0e80301ad36ed34bf69](https://gist.github.com/tastyminerals/f6764ff472c8e0e80301ad36ed34bf69)

Julia 1.4.0 benchmark code is here: [https://gist.github.com/tastyminerals/86eb070f4ea28a1dda2578de88d0e9f2](https://gist.github.com/tastyminerals/86eb070f4ea28a1dda2578de88d0e9f2)

Thank you!

---

<div class="post-metadata">

### Author: ![Tasty\_Minerals](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tasty_minerals/32/13698_2.png) [@Tasty\_Minerals](https://discourse.julialang.org/u/Tasty_Minerals)
#### Post date: [March 29, 2020, 8:09pm UTC](https://discourse.julialang.org/t/benchmarking-julia-vs-numpy/36695/2 "2020-03-29T20:09:35Z")

</div>

Numpy benchmark code is here: [Numpy benchmarks · GitHub](https://gist.github.com/tastyminerals/02a470b5ab8735e9890ad2ac9668e14d)

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [March 29, 2020, 8:11pm UTC](https://discourse.julialang.org/t/benchmarking-julia-vs-numpy/36695/3 "2020-03-29T20:11:12Z")

</div>

It matters a lot how these benchmarks are executed. Numpy uses a blas library with a certain number of threads, executing for instance matrix-matrix multiplication in parallel. If one does not start Julia with multiple threads, it is hobbled.

There are several threads on this forum where you will find more information. Search for numpy and benchmark.

---

<div class="post-metadata">

### Author: ![Tasty\_Minerals](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tasty_minerals/32/13698_2.png) [@Tasty\_Minerals](https://discourse.julialang.org/u/Tasty_Minerals)
#### Post date: [March 29, 2020, 8:13pm UTC](https://discourse.julialang.org/t/benchmarking-julia-vs-numpy/36695/4 "2020-03-29T20:13:39Z")

</div>

> [@PetrKryslUCSD](#):
>
> numpy and benchmark

Thank you.  
Both were using multiple threads.  
However, NumPy used MKL as backend (Linux conda).  
Julia used OpenBLAS (Linux).

---

<div class="post-metadata">

### Author: ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)
#### Post date: [March 29, 2020, 8:18pm UTC](https://discourse.julialang.org/t/benchmarking-julia-vs-numpy/36695/5 "2020-03-29T20:18:30Z")

</div>

You are using non-constant globals.

[https://docs.julialang.org/en/v1/manual/performance-tips/](https://docs.julialang.org/en/v1/manual/performance-tips/)

---

<div class="post-metadata">

### Author: ![improbable22](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/improbable22/32/5464_2.png) [@improbable22](https://discourse.julialang.org/u/improbable22)
#### Post date: [March 29, 2020, 8:24pm UTC](https://discourse.julialang.org/t/benchmarking-julia-vs-numpy/36695/6 "2020-03-29T20:24:12Z")

</div>

Also, `test2()` is doing matrix multiplication.

And anyway, as you seem to know, all the work is done by the library. If you run both with the same library, then you will be benchmarking your benchmarking technique…

FWIW, on my computer the difference seems much less than 10x for test4, is it really this different on your computer, or was there a copy-paste error?

```julia
julia> @btime $float_matrixA * $float_matrixC; # test4, MKL — 0.001_856 in your table
  946.013 μs (2 allocations: 1.91 MiB)

julia> @btime $float_matrixA * $float_matrixC; # test4, OpenBLAS — 0.019_880 in your table
  1.191 ms (2 allocations: 1.91 MiB)

```

---

<div class="post-metadata">

### Author: ![Tasty\_Minerals](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tasty_minerals/32/13698_2.png) [@Tasty\_Minerals](https://discourse.julialang.org/u/Tasty_Minerals)
#### Post date: [March 29, 2020, 8:28pm UTC](https://discourse.julialang.org/t/benchmarking-julia-vs-numpy/36695/7 "2020-03-29T20:28:14Z")

</div>

I see, thank you. Just retested with all globals prepended with `const`. The results improved a but not drastically (still slower than NumPy).

---

<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: [March 29, 2020, 8:41pm UTC](https://discourse.julialang.org/t/benchmarking-julia-vs-numpy/36695/8 "2020-03-29T20:41:18Z")

</div>

As @improbable22 said, you’re comparing different operations. This line: [https://gist.github.com/tastyminerals/86eb070f4ea28a1dda2578de88d0e9f2#file-julia\_bench-jl-L31](https://gist.github.com/tastyminerals/86eb070f4ea28a1dda2578de88d0e9f2#file-julia_bench-jl-L31) is labeled as “element-wise multiplication”, but you’re actually doing matrix multiplication and then comparing it with element-wise multiplication in numpy.

---

<div class="post-metadata">

### Author: ![Tasty\_Minerals](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tasty_minerals/32/13698_2.png) [@Tasty\_Minerals](https://discourse.julialang.org/u/Tasty_Minerals)
#### Post date: [March 29, 2020, 8:44pm UTC](https://discourse.julialang.org/t/benchmarking-julia-vs-numpy/36695/9 "2020-03-29T20:44:17Z")

</div>

Indeed! Corrected.  
Sorry, didn’t understand your last sentence. I thought just `@btime` is enough for such simple tests.

---

<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: [March 29, 2020, 9:21pm UTC](https://discourse.julialang.org/t/benchmarking-julia-vs-numpy/36695/10 "2020-03-29T21:21:38Z")

</div>

If you want to use MKL with Julia, the easiest way is to install [MKL.jl](https://github.com/JuliaComputing/MKL.jl).
