# Julia slower than Matlab & Python? No

**URL:** <https://discourse.julialang.org/t/julia-slower-than-matlab-python-no/33128>\
**Category:** Performance\
**Tags:** economics, tensorflow, matlab, pytorch\
**Created:** [January 8, 2020, 11:57pm UTC](https://discourse.julialang.org/t/julia-slower-than-matlab-python-no/33128 "2020-01-08T23:57:37Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![Iulian.Cioarca](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iulian.cioarca/32/30166_2.png) [@Iulian.Cioarca](https://discourse.julialang.org/u/Iulian.Cioarca)\
**Post date:** [January 9, 2020, 9:09am UTC](https://discourse.julialang.org/t/julia-slower-than-matlab-python-no/33128/21 "2020-01-09T09:09:06Z")

</div>

Here is the graph of the original results with log Y scale, for better viewing. ![benchmark](https://global.discourse-cdn.com/julialang/original/3X/e/2/e2b9b308b750dc5dd651ccf02e0a15c477f94569.jpeg)

---

<div class="post-metadata">

**Author:** ![Zach\_Christensen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zach_christensen/32/7220_2.png) [@Zach\_Christensen](https://discourse.julialang.org/u/Zach_Christensen)\
**Post date:** [January 9, 2020, 9:24am UTC](https://discourse.julialang.org/t/julia-slower-than-matlab-python-no/33128/22 "2020-01-09T09:24:42Z")

</div>

I would imagine that the one measurement of `@time` isn’t always the best way to benchmark things, even if it is the second run.

```julia
using BenchmarkTools

julia> @btime C.^0.3
  9.425 s (2 allocations: 3.48 GiB)

```

---

<div class="post-metadata">

**Author:** ![Sijun](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@Sijun](https://discourse.julialang.org/u/Sijun)\
**Post date:** [January 9, 2020, 10:49am UTC](https://discourse.julialang.org/t/julia-slower-than-matlab-python-no/33128/23 "2020-01-09T10:49:15Z")

</div>

I think they need to be compared on the same machine.

I tried with smaller array and Julia version still take twice as longer (I used @btime for measuring):

```julia
C = np.random.rand(100,100,100)

%%timeit

C**0.3

29.2 ms ± 539 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

```

while

```julia
C = rand(100,100,100)

@btime C.^0.3

67.824 ms (4 allocations: 7.63 MiB)

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [January 9, 2020, 1:04pm UTC](https://discourse.julialang.org/t/julia-slower-than-matlab-python-no/33128/24 "2020-01-09T13:04:58Z")

</div>

Could you try

```julia
@btime $C.^0.3

```

?

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [January 9, 2020, 1:11pm UTC](https://discourse.julialang.org/t/julia-slower-than-matlab-python-no/33128/25 "2020-01-09T13:11:52Z")

</div>

I have no idea why you are getting this. See what I got in my machine:

```python
In [1]: import numpy as np

In [2]: C = np.random.rand(100,100,100)

In [3]: %%timeit
   ...: C**0.3
   ...:
   ...:
17.5 ms ± 127 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

```

```julia
julia> using BenchmarkTools

julia> C = rand(100,100,100);

julia> @btime C.^0.3;
  17.387 ms (4 allocations: 7.63 MiB)

```

---

<div class="post-metadata">

**Author:** ![Sijun](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@Sijun](https://discourse.julialang.org/u/Sijun)\
**Post date:** [January 9, 2020, 1:17pm UTC](https://discourse.julialang.org/t/julia-slower-than-matlab-python-no/33128/26 "2020-01-09T13:17:00Z")

</div>

Here it is:

```julia
julia> @btime $C.^0.3;
  74.537 ms (2 allocations: 7.63 MiB)

```

---

<div class="post-metadata">

**Author:** ![Sijun](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@Sijun](https://discourse.julialang.org/u/Sijun)\
**Post date:** [January 9, 2020, 1:19pm UTC](https://discourse.julialang.org/t/julia-slower-than-matlab-python-no/33128/27 "2020-01-09T13:19:16Z")

</div>

Really strange… I am getting the same result at office and at home.

---

<div class="post-metadata">

**Author:** ![Albert\_Zevelev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albert_zevelev/32/11844_2.png) [@Albert\_Zevelev](https://discourse.julialang.org/u/Albert_Zevelev)\
**Post date:** [January 9, 2020, 3:37pm UTC](https://discourse.julialang.org/t/julia-slower-than-matlab-python-no/33128/28 "2020-01-09T15:37:09Z")

</div>

> [@longemen3000](#):
>
> function u(c, γ) #add the broadcast in the call? return c.^(1 - γ) / (1 - γ) end

what exactly does that mean?

```
    Vd_target = u(def_y, γ) + β * (θ * EVc[:, zero_ind] + (1 - θ) * EVd[:]) #calling a[:] allocates a new array

```

how can that be fixed?

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [January 9, 2020, 3:39pm UTC](https://discourse.julialang.org/t/julia-slower-than-matlab-python-no/33128/29 "2020-01-09T15:39:37Z")

</div>

Actually, I see a similar performance difference as @Sijun, about a factor of 2 between C and Julia. Strange. Is this perhaps some MKL/LIBM or multithreading issue (just guessing)?

---

<div class="post-metadata">

**Author:** ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)\
**Post date:** [January 9, 2020, 3:42pm UTC](https://discourse.julialang.org/t/julia-slower-than-matlab-python-no/33128/30 "2020-01-09T15:42:24Z")

</div>

There is already a PR by @tim.holy which shows how to do this: [Speed up Julia code by timholy · Pull Request #2 · vduarte/benchmarkingML · GitHub](https://github.com/vduarte/benchmarkingML/pull/2)

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [January 9, 2020, 3:49pm UTC](https://discourse.julialang.org/t/julia-slower-than-matlab-python-no/33128/31 "2020-01-09T15:49:07Z")

</div>

In the paper linked in the original post, they compare a few different GPU implementations but don’t mention that Julia can run in GPUs. Is there anyone around with a GPU that wants to show off CuArrays.jl? It seems like a good fit for this problem.

---

<div class="post-metadata">

**Author:** ![Zach\_Christensen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zach_christensen/32/7220_2.png) [@Zach\_Christensen](https://discourse.julialang.org/u/Zach_Christensen)\
**Post date:** [January 9, 2020, 3:49pm UTC](https://discourse.julialang.org/t/julia-slower-than-matlab-python-no/33128/32 "2020-01-09T15:49:35Z")

</div>

I don’t have an MKL build of Julia.

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [January 9, 2020, 3:51pm UTC](https://discourse.julialang.org/t/julia-slower-than-matlab-python-no/33128/33 "2020-01-09T15:51:40Z")

</div>

Many Python distributions use MKL by default. Having VML hooked up to Python would explain these differences I think.

---

<div class="post-metadata">

**Author:** ![jtackm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jtackm/32/4784_2.png) [@jtackm](https://discourse.julialang.org/u/jtackm)\
**Post date:** [January 9, 2020, 3:57pm UTC](https://discourse.julialang.org/t/julia-slower-than-matlab-python-no/33128/34 "2020-01-09T15:57:28Z")

</div>

Numpy and Julia versions run at close to the same speed on both my Macbook and a Ubuntu workstation (default Julia 1.3 vs miniconda3 /w MKL)

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [January 9, 2020, 6:04pm UTC](https://discourse.julialang.org/t/julia-slower-than-matlab-python-no/33128/35 "2020-01-09T18:04:49Z")

</div>

Should have said it before: I’m on Windows 10 on this machine.

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [January 9, 2020, 6:48pm UTC](https://discourse.julialang.org/t/julia-slower-than-matlab-python-no/33128/36 "2020-01-09T18:48:09Z")

</div>

I made some [modifications to Tim Holy’s PR](https://github.com/timholy/benchmarkingML/pull/1) and found that by using Strided.jl, I was able to beat Python/Numpy and Matlab for all sizes other than 151. Here are my timings on the same model macbook as the one the authors used:

```julia
151: 326.1284828186035
351: 308.24360847473145
551: 690.5834913253784
751: 1231.9454908370972
951: 1912.0723962783813
1151: 5935.046696662903
1351: 18267.05288887024
1551: 29274.00109767914

```

I believe this suggests that the difference was that Python and Matlab were multithreading the broadcast operations across the two available threads whereas julia does not multi-thread broadcast unless you use something like Strided.jl.

The second time it’s run, the timings improve. I’m not sure why exactly that is, it seems like all the JIT overhead should have been hit before the benchmarking loop in the first function call, but nonetheless here is the second run timing:

```julia
julia> include("julia.jl")
151: 89.70789909362793
351: 288.47689628601074
551: 686.4475965499878
751: 1315.5532121658325
951: 2086.0692977905273
1151: 5694.838190078735
1351: 18829.65850830078
1551: 31184.902906417847

```

---

<div class="post-metadata">

**Author:** ![pkofod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pkofod/32/2179_2.png) [@pkofod](https://discourse.julialang.org/u/pkofod)\
**Post date:** [January 9, 2020, 7:10pm UTC](https://discourse.julialang.org/t/julia-slower-than-matlab-python-no/33128/38 "2020-01-09T19:10:24Z")

</div>

Publish soon? This is already published in JEDC [Benchmarking machine-learning software and hardware for quantitative economics - ScienceDirect](https://www.sciencedirect.com/science/article/pii/S0165188919301939) .

---

<div class="post-metadata">

**Author:** ![Albert\_Zevelev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albert_zevelev/32/11844_2.png) [@Albert\_Zevelev](https://discourse.julialang.org/u/Albert_Zevelev)\
**Post date:** [January 9, 2020, 7:13pm UTC](https://discourse.julialang.org/t/julia-slower-than-matlab-python-no/33128/39 "2020-01-09T19:13:15Z")

</div>

The author’s website says “Conditionally Accepted”

---

<div class="post-metadata">

**Author:** ![pkofod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pkofod/32/2179_2.png) [@pkofod](https://discourse.julialang.org/u/pkofod)\
**Post date:** [January 9, 2020, 7:29pm UTC](https://discourse.julialang.org/t/julia-slower-than-matlab-python-no/33128/40 "2020-01-09T19:29:52Z")

</div>

Apparently not conditional on checking with the Julia community first 🙂

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [January 9, 2020, 7:57pm UTC](https://discourse.julialang.org/t/julia-slower-than-matlab-python-no/33128/41 "2020-01-09T19:57:10Z")

</div>

Suddenly I know how to get optimized programs for free…  
Step 1: Code up programs for my paper in Matlab  
Step 2: Write a naive version in Julia  
Step 3: Post on Julia Discourse  
Step 4: …  
Step 5: Publish!

[Previous page](https://discourse.julialang.org/t/julia-slower-than-matlab-python-no/33128.md?page=1)

[Next page](https://discourse.julialang.org/t/julia-slower-than-matlab-python-no/33128.md?page=3)
