# Julia vs Matlab - building a Jacobian matrix

**URL:** <https://discourse.julialang.org/t/julia-vs-matlab-building-a-jacobian-matrix/14495>\
**Category:** New to Julia\
**Created:** [September 3, 2018, 6:11pm UTC](https://discourse.julialang.org/t/julia-vs-matlab-building-a-jacobian-matrix/14495 "2018-09-03T18:11:42Z")\
**Posts on this page:** 11\
**Page:** 2

<div class="post-metadata">

**Author:** ![OlivierHnt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/olivierhnt/32/6227_2.png) [@OlivierHnt](https://discourse.julialang.org/u/OlivierHnt)\
**Post date:** [September 3, 2018, 8:51pm UTC](https://discourse.julialang.org/t/julia-vs-matlab-building-a-jacobian-matrix/14495/21 "2018-09-03T20:51:19Z")

</div>

I see, very clear once again! I also guess from this that in the case of nested loops:

```julia
@inbounds for i in 1:10
              for j in 1:10
                  ... 
              end
           end

```

the function `@inbounds` _do not_ apply for all loops and I should call it again when needed.  
I did no know about the `begin` environment. Thank you for that as well!

---

<div class="post-metadata">

**Author:** ![mtsch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mtsch/32/2072_2.png) [@mtsch](https://discourse.julialang.org/u/mtsch)\
**Post date:** [September 3, 2018, 8:52pm UTC](https://discourse.julialang.org/t/julia-vs-matlab-building-a-jacobian-matrix/14495/22 "2018-09-03T20:52:26Z")

</div>

Another performance tip when using nested for loops:  
Make sure the inner loop traverses the columns of your matrix (a.k.a. the first index).  
This can make a dramatic difference, especially when your matrices are large and when the logic using them is simple.

See this example:

```julia
using BenchmarkTools

function f1(n)
    res = zeros(n, n)
    for i in 1:n, j in 1:n
        res[i, j] = i + j
    end
    res
end

function f2(n)
    res = zeros(n, n)
    for j in 1:n, i in 1:n
        res[i, j] = i + j
    end
    res
end

@btime f1(2000);
  58.354 ms (2 allocations: 30.52 MiB)

@btime f2(2000);
  12.590 ms (2 allocations: 30.52 MiB)

```

---

<div class="post-metadata">

**Author:** ![OlivierHnt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/olivierhnt/32/6227_2.png) [@OlivierHnt](https://discourse.julialang.org/u/OlivierHnt)\
**Post date:** [September 3, 2018, 8:53pm UTC](https://discourse.julialang.org/t/julia-vs-matlab-building-a-jacobian-matrix/14495/23 "2018-09-03T20:53:46Z")

</div>

I did not know about this! Thank you very much for the tip!

---

<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:** [September 3, 2018, 9:11pm UTC](https://discourse.julialang.org/t/julia-vs-matlab-building-a-jacobian-matrix/14495/24 "2018-09-03T21:11:51Z")

</div>

> [@rdeits](#):
>
> ```julia
> julia> @btime sum_of_diff_2($(rand(10_000)))
> 1.394 μs (0 allocations: 0 bytes)
> 
> ```

Just to make a point:

```julia
julia> sum_of_diff_3(x) = last(x) - first(x)
julia> @btime sum_of_diff_3($(rand(10_000)));
  1.849 ns (0 allocations: 0 bytes)

```

(this computes the same thing).

So the sum of diffs is a toy example, I just wanted to underscore that if you _really_ want to speed things up, you look for a better _algorithm_. In many cases that will trump all tricks and optimizations. See if you are calculating things that you can do on paper, or maybe skip entirely.

---

<div class="post-metadata">

**Author:** ![affans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/affans/32/11911_2.png) [@affans](https://discourse.julialang.org/u/affans)\
**Post date:** [September 4, 2018, 12:26am UTC](https://discourse.julialang.org/t/julia-vs-matlab-building-a-jacobian-matrix/14495/25 "2018-09-04T00:26:35Z")

</div>

Hi @rdeits  
Your code here using `btime` says 0 allocations. If I use `@time` I get

```julia
@time sum_of_diff_2(rand(100))
  0.000006 seconds (6 allocations: 1.047 KiB)

```

Why the apparent difference?

---

<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:** [September 4, 2018, 1:03am UTC](https://discourse.julialang.org/t/julia-vs-matlab-building-a-jacobian-matrix/14495/26 "2018-09-04T01:03:59Z")

</div>

You’re measuring the time and allocation of the construction of the `rand(100)` vector. In my example I used `$` to interpolate that vector into the benchmarked expression: [https://github.com/JuliaCI/BenchmarkTools.jl/blob/master/doc/manual.md#interpolating-values-into-benchmark-expressions](https://github.com/JuliaCI/BenchmarkTools.jl/blob/master/doc/manual.md#interpolating-values-into-benchmark-expressions)

---

<div class="post-metadata">

**Author:** ![colintbowers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/colintbowers/32/8033_2.png) [@colintbowers](https://discourse.julialang.org/u/colintbowers)\
**Post date:** [September 5, 2018, 12:22am UTC](https://discourse.julialang.org/t/julia-vs-matlab-building-a-jacobian-matrix/14495/27 "2018-09-05T00:22:17Z")

</div>

Matlab also uses column-major ordering, so mtsch’s tip actually applies to both languages 🙂 (you’ll note that summing a matrix column in Matlab is about 5 times faster than summing an equivalent length row)

---

<div class="post-metadata">

**Author:** ![Juser](https://avatars.discourse-cdn.com/v4/letter/j/34f0e0/32.png) [@Juser](https://discourse.julialang.org/u/Juser)\
**Post date:** [September 5, 2018, 1:36am UTC](https://discourse.julialang.org/t/julia-vs-matlab-building-a-jacobian-matrix/14495/28 "2018-09-05T01:36:43Z")

</div>

A note on `@inbounds`, I want to emphasize @rdeits suggestion that you not implement `@inbounds` in your code until you’re certain that it is correct. I cannot count the number of hours I have spent tracking down a stupid non-deterministic bug because I used `@inbounds` without knowing that in some cases it would index out of bounds and just grab the next thing available in memory. These are the hardest types of bugs to catch because their behavior can change each time you run the code depending on what is in memory “near” the thing you’re indexing into. Also, I’ve noticed that on V0.7 the benefits to `@inbounds` appear to be lower than in previous versions of Julia. I have no formal proof of this other than my own small amount of observational data.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [September 5, 2018, 2:48am UTC](https://discourse.julialang.org/t/julia-vs-matlab-building-a-jacobian-matrix/14495/29 "2018-09-05T02:48:44Z")

</div>

> [@Juser](#):
>
> Also, I’ve noticed that on V0.7 the benefits to `@inbounds` appear to be lower than in previous versions of Julia.

There was a very nice PR that fixed this ([https://github.com/JuliaLang/julia/pull/27386](https://github.com/JuliaLang/julia/pull/27386)).

Note that you can turn off your `@inbounds` by starting julia with `--check-bounds=yes` in case you suspect that you have made a mistake somewhere.

---

<div class="post-metadata">

**Author:** ![Aquaman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aquaman/32/6586_2.png) [@Aquaman](https://discourse.julialang.org/u/Aquaman)\
**Post date:** [March 28, 2019, 7:30pm UTC](https://discourse.julialang.org/t/julia-vs-matlab-building-a-jacobian-matrix/14495/30 "2019-03-28T19:30:13Z")

</div>

It is very hard to say that, some times the numerical job depends on the third parts code (plotting etc)

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [March 28, 2019, 7:47pm UTC](https://discourse.julialang.org/t/julia-vs-matlab-building-a-jacobian-matrix/14495/31 "2019-03-28T19:47:51Z")

</div>

Hey and welcome to the forum, please try not to “necropost” (post in very old threads) unless there is a strong reason to. It is often better to start a new thread than necroposting.

[Previous page](https://discourse.julialang.org/t/julia-vs-matlab-building-a-jacobian-matrix/14495.md?page=1)
