# Matlab versus Julia

**URL:** <https://discourse.julialang.org/t/matlab-versus-julia/64469>\
**Category:** General Usage\
**Created:** [July 11, 2021, 7:21pm UTC](https://discourse.julialang.org/t/matlab-versus-julia/64469 "2021-07-11T19:21:17Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![asaretto](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/asaretto/32/27027_2.png) [@asaretto](https://discourse.julialang.org/u/asaretto)\
**Post date:** [July 11, 2021, 7:21pm UTC](https://discourse.julialang.org/t/matlab-versus-julia/64469/1 "2021-07-11T19:21:17Z")

</div>

Hi guys, I am new to Julia. I have been using Matlab for many years. I have been told that Julia can offer great improvements in terms of performance. After doing a very preliminary intro course on Julia I started hacking around and to try to understand how to write code that is actually faster. So far all the benchmark attempts I have tried failed miserably, in the sense that Matlab is always much much faster. Obviously I must be doing something wrong. Can anybody give some tips?

For example, I thought Julia would be much faster at running loops. So I tried the following in both Matlab and Julia (on the same computer)

aa = zeros(Float32,100);  
bb = ones(Float32,100);  
cc = ones(Float32,100);  
using TickTock  
tick()  
for i = 1:100  
for j = 1:100  
for k = 1:100  
cc = aa.\*bb;  
end  
end  
end  
t1 = tock()

The Julia code, which I am running on Atom, runs in about 0.8 secs. The equivalent Matlab code runs in 0.1 secs. What am I missing?

thanks so much

---

<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:** [July 11, 2021, 7:34pm UTC](https://discourse.julialang.org/t/matlab-versus-julia/64469/2 "2021-07-11T19:34:07Z")

</div>

See: [Performance Tips · The Julia Language](https://docs.julialang.org/en/v1/manual/performance-tips/)

The very first tip in particular.

In this example, do:

> [@asaretto](#):
>
> ```julia
> aa = zeros(Float32,100);
> bb = ones(Float32,100);
> cc = ones(Float32,100);
> 
> function test!(aa,bb,cc)
> for i = 1:100
> for j = 1:100
> for k = 1:100
> cc .= aa.*bb;
> end
> end
> end
> end
> 
> using BenchmarkTools
> @btime test!($aa,$bb,$cc)
> 
> ```

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [July 11, 2021, 7:40pm UTC](https://discourse.julialang.org/t/matlab-versus-julia/64469/3 "2021-07-11T19:40:23Z")

</div>

I think there are some typos `vc` → `cc` and `test` → `test!` But if matlab can do that in 100ms something’s still wrong, it takes 700ms for me.

---

<div class="post-metadata">

**Author:** ![asaretto](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/asaretto/32/27027_2.png) [@asaretto](https://discourse.julialang.org/u/asaretto)\
**Post date:** [July 11, 2021, 7:42pm UTC](https://discourse.julialang.org/t/matlab-versus-julia/64469/4 "2021-07-11T19:42:47Z")

</div>

First of all, thanks for taking the time. And yes, I had caught the typos. Easy fix  
However, now this takes 5 seconds to run, as opposed to 0.8. Is that what happens on your computer as well?

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [July 11, 2021, 7:43pm UTC](https://discourse.julialang.org/t/matlab-versus-julia/64469/5 "2021-07-11T19:43:46Z")

</div>

Make sure you’re using BenchmarkTools `@btime` to get a good measurement. Julia’s function calls are always slow on their first execution because they are being compiled.

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [July 11, 2021, 7:45pm UTC](https://discourse.julialang.org/t/matlab-versus-julia/64469/6 "2021-07-11T19:45:40Z")

</div>

@asaretto can you show your matlab code?

---

<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:** [July 11, 2021, 7:46pm UTC](https://discourse.julialang.org/t/matlab-versus-julia/64469/7 "2021-07-11T19:46:33Z")

</div>

Sorry for the typos. I am writing from my cell phone, so I cannot test it here. Can you post exactly your code and the Matlab code for us to see if they are doing the same?

That should absolutely not take 5s.

---

<div class="post-metadata">

**Author:** ![asaretto](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/asaretto/32/27027_2.png) [@asaretto](https://discourse.julialang.org/u/asaretto)\
**Post date:** [July 11, 2021, 7:46pm UTC](https://discourse.julialang.org/t/matlab-versus-julia/64469/8 "2021-07-11T19:46:42Z")

</div>

Matlab code:

aa = zeros(100,1);  
bb = ones(100,1);  
cc = ones(100,1);  
tic  
for i = 1:100  
for j = 1:100  
for k = 1:100  
cc = aa.\*bb;  
end  
end  
end  
t1 = toc

Julia code:

> Blockquote

using BenchmarkTools  
aa = zeros(Int16,100);  
bb = ones(Int16,100);  
cc = ones(Int16,100);  
function test(as,bb,vc)  
for i = 1:100  
for j = 1:100  
for k = 1:100  
cc = aa.\*bb;  
end  
end  
end  
end  
@btime test($aa,$bb,$cc)

> Blockquote

Now it says it takes 600ms, but it takes more than that to print the answer on the screen(!?)

---

<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:** [July 11, 2021, 7:49pm UTC](https://discourse.julialang.org/t/matlab-versus-julia/64469/9 "2021-07-11T19:49:01Z")

</div>

> [@asaretto](#):
>
> cc = aa.\*bb;

The dot there means the same thing as in Julia?

Ps. Use better triple back ticks to quote the code.

---

<div class="post-metadata">

**Author:** ![asaretto](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/asaretto/32/27027_2.png) [@asaretto](https://discourse.julialang.org/u/asaretto)\
**Post date:** [July 11, 2021, 7:50pm UTC](https://discourse.julialang.org/t/matlab-versus-julia/64469/10 "2021-07-11T19:50:50Z")

</div>

From what (little) I understand the element by element multiplication in Matlab is the same as the broadcasting in Julia

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [July 11, 2021, 7:51pm UTC](https://discourse.julialang.org/t/matlab-versus-julia/64469/11 "2021-07-11T19:51:47Z")

</div>

> [@lmiq](#):
>
> > [@asaretto](#):
> >
> > cc = aa.\*bb;

Try

```julia
cc .= aa.*bb

```

This should avoid some allocations.

---

<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:** [July 11, 2021, 7:53pm UTC](https://discourse.julialang.org/t/matlab-versus-julia/64469/12 "2021-07-11T19:53:27Z")

</div>

There was yet another typo. This is what you should get:

```julia
julia> aa = zeros(Float32,100);

julia> bb = ones(Float32,100);

julia> cc = ones(Float32,100);

julia> function test!(aa,bb,cc)
         for i = 1:100
           for j = 1:100
             for k = 1:100
               cc .= aa.*bb;
             end
           end
         end
       end
test! (generic function with 1 method)

julia> using BenchmarkTools

julia> @btime test!($aa,$bb,$cc)
  15.543 ms (0 allocations: 0 bytes)

julia> 

```

Note a second `.` in ` cc .= aa .* bb` , meaning that you are updating `cc` and not creating a new one at every iteration. (this can be written also as `@. cc = aa * bb`).

> [@asaretto](#):
>
> Now it says it takes 600ms, but it takes more than that to print the answer on the screen(!?)

This is because `@btime` executes the function many times to obtain an accurate measure of the performance. Also, the function, here `test!` is compiled in the first call, so you get an innacurate measure of its performance by measuring only _one_ call of it, if it is a fast function. For example, starting from a fresh section:

```julia
julia> aa = rand(Float32,100);

julia> bb = rand(Float32,100);

julia> cc = ones(Float32,100);

julia> function test!(aa,bb,cc)
         for i = 1:100
           for j = 1:100
             for k = 1:100
               cc .= aa .* bb;
             end
           end
         end
       end
test! (generic function with 1 method)

julia> @time test!(aa,bb,cc) # first run
  0.100118 seconds (221.07 k allocations: 12.956 MiB, 9.55% gc time, 83.58% compilation time)

julia> @time test!(aa,bb,cc) # second and following runs
  0.025998 seconds

```

This is using the `@time` macro. The `@btime` macro does that multiple running for you, and reports the minimum time obtained (which is a more stable measure of performance).

---

<div class="post-metadata">

**Author:** ![bernhard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernhard/32/2619_2.png) [@bernhard](https://discourse.julialang.org/u/bernhard)\
**Post date:** [July 11, 2021, 7:53pm UTC](https://discourse.julialang.org/t/matlab-versus-julia/64469/13 "2021-07-11T19:53:50Z")

</div>

I would suggest that you try out a „real life“ example from your everyday work. That will be much more helpful than some generic example (the one being shown here does not seem very interesting/meaningful to me)

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [July 11, 2021, 7:57pm UTC](https://discourse.julialang.org/t/matlab-versus-julia/64469/14 "2021-07-11T19:57:44Z")

</div>

Note also that comparing performance for a _single_ vectorized operation is pretty uninteresting and unlikely to show much in the way of performance gains. See also [Comparing Numba and Julia for a complex matrix computation - #3 by stevengj](https://discourse.julialang.org/t/comparing-numba-and-julia-for-a-complex-matrix-computation/63703/3)

---

<div class="post-metadata">

**Author:** ![asaretto](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/asaretto/32/27027_2.png) [@asaretto](https://discourse.julialang.org/u/asaretto)\
**Post date:** [July 11, 2021, 8:01pm UTC](https://discourse.julialang.org/t/matlab-versus-julia/64469/15 "2021-07-11T20:01:15Z")

</div>

There is one considerable difference relative to what you are getting, which might be one source of problems. When I run the very last part (the benchmark) I get the following:

julia\> @btime test!($aa,$bb,$cc)  
551.124 ms (2000000 allocations: 61.04 MiB)

Aside from the time that can be machine specific, it looks like you have 0 allocations, while I have 2M. Is that a problem?

---

<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:** [July 11, 2021, 8:02pm UTC](https://discourse.julialang.org/t/matlab-versus-julia/64469/16 "2021-07-11T20:02:46Z")

</div>

> [@asaretto](#):
>
> function test(as,bb,vc)

There is a typo there: `as` should be `aa` (my fault on the original post).

> [@asaretto](#):
>
> Aside from the time that can be machine specific, it looks like you have 0 allocations, while I have 2M. Is that a problem?

That above. `aa` was being considered a global variable inside the function because the name of the parameter was wrong. (I figured it readily when I noticed those allocations, that shouldn’t be there).

(and the broadcasting of setting `cc`, the first `dot` I mentioned there, `cc .= aa .* bb`).

---

<div class="post-metadata">

**Author:** ![asaretto](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/asaretto/32/27027_2.png) [@asaretto](https://discourse.julialang.org/u/asaretto)\
**Post date:** [July 11, 2021, 8:15pm UTC](https://discourse.julialang.org/t/matlab-versus-julia/64469/17 "2021-07-11T20:15:13Z")

</div>

That did make it faster. now it takes 125ms on my laptop, which is at least about the same as Matlab.

Ok, say that I want to try something more complicated. The type of staff that I do is solve/calibrate numerical models whose solution is essentially the solution to a fix point problem.

How should I think about Julia in terms of the things it does more efficiently? Loops always? No vectorizations? Is there a must read book/article/blog?

thanks again guys, I really appreciate it

---

<div class="post-metadata">

**Author:** ![zdenek\_hurak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zdenek_hurak/32/53118_2.png) [@zdenek\_hurak](https://discourse.julialang.org/u/zdenek_hurak)\
**Post date:** [July 11, 2021, 8:24pm UTC](https://discourse.julialang.org/t/matlab-versus-julia/64469/18 "2021-07-11T20:24:13Z")

</div>

> [@asaretto](#):
>
> Is there a must read book/article/blog?

- [Performance Tips · The Julia Language](https://docs.julialang.org/en/v1/manual/performance-tips/)
- some lectures from [https://julia.quantecon.org/index\_toc.html](https://julia.quantecon.org/index_toc.html). Certainly [4. Arrays, Tuples, Ranges, and Other Fundamental Types — Quantitative Economics with Julia](https://julia.quantecon.org/getting_started_julia/fundamental_types.html) and [5. Introduction to Types and Generic Programming — Quantitative Economics with Julia](https://julia.quantecon.org/getting_started_julia/introduction_to_types.html).
- The book [https://www.packtpub.com/product/hands-on-design-patterns-and-best-practices-with-julia/9781838648817](https://www.packtpub.com/product/hands-on-design-patterns-and-best-practices-with-julia/9781838648817)

---

<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:** [July 11, 2021, 8:43pm UTC](https://discourse.julialang.org/t/matlab-versus-julia/64469/19 "2021-07-11T20:43:19Z")

</div>

> [@asaretto](#):
>
> How should I think about Julia in terms of the things it does more efficiently?

I think it is easier to say where you should not expect a significant gain: if the expensive part of the Matlab code is a call to a library which is optimized in a lower level language, the Julia code (which may be eventually a call to the same library, many times) will be equally fast (or slow). Other times a pure Julia library for the same task, or your implementation, adapted to your problem, can be faster.

Julia can be efficient as a low level language as C++ or Fortran, not more, not less.

(Ps. Probably you are still missing the dot or the fixed typo on `as` to get that timing. As a good practice it is always nice to post the last code, so others can suggest improvements and fixes. If you have a slightly more realistic example, from there there are other possible optimizations and learning to parallelize the loops, that are worth exploring).

I also have some notes, which I write as I learn: [Home · JuliaNotes.jl](https://m3g.github.io/JuliaNotes.jl/stable/)

---

<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:** [July 11, 2021, 8:52pm UTC](https://discourse.julialang.org/t/matlab-versus-julia/64469/20 "2021-07-11T20:52:31Z")

</div>

I think it is possible that Matlab realized that the result was not dependent on the index i, j, k, and collapsed the whole looping into a single multiplication of matrices.

Edit: This does not happen in Matlab. I checked.

[Next page](https://discourse.julialang.org/t/matlab-versus-julia/64469.md?page=2)
