# Understanding Julia performance in simple finite difference code

**URL:** <https://discourse.julialang.org/t/understanding-julia-performance-in-simple-finite-difference-code/11472>\
**Category:** New to Julia\
**Created:** [June 6, 2018, 2:16pm UTC](https://discourse.julialang.org/t/understanding-julia-performance-in-simple-finite-difference-code/11472 "2018-06-06T14:16:40Z")\
**Posts on this page:** 19\
**Page:** 2

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [June 7, 2018, 3:44am UTC](https://discourse.julialang.org/t/understanding-julia-performance-in-simple-finite-difference-code/11472/21 "2018-06-07T03:44:59Z")

</div>

For the last part, have you rebuilt your system image (or compiled Julia from source) and set the optimization level to `-O3`? Julia won’t use SIMD automatically without that, and that could be the final kicker.

---

<div class="post-metadata">

**Author:** ![lwhitefox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lwhitefox/32/4363_2.png) [@lwhitefox](https://discourse.julialang.org/u/lwhitefox)\
**Post date:** [June 7, 2018, 3:49am UTC](https://discourse.julialang.org/t/understanding-julia-performance-in-simple-finite-difference-code/11472/22 "2018-06-07T03:49:34Z")

</div>

> [@ChrisRackauckas](#):
>
> Think of it as a function `*(::Array,::Float64)` , how would you write that function?

My thinking at this point is largely influenced by Mathematica, where multiplying a list (1d array) by a scalar (e.g., Float64) simply applies the operation to each element in the list (multiplying by the scalar, presumably optimized for the Mathematica way of doing things). So, I’m having to retrain my thinking in several ways as I move into Julia (and away from Mathematica, C++, C, etc.).

Thanks for the tips - I see you also modified another line to properly apply broadcasting so I’ll add that too, but I think I’m now making good progress. 👍

---

<div class="post-metadata">

**Author:** ![lwhitefox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lwhitefox/32/4363_2.png) [@lwhitefox](https://discourse.julialang.org/u/lwhitefox)\
**Post date:** [June 7, 2018, 3:51am UTC](https://discourse.julialang.org/t/understanding-julia-performance-in-simple-finite-difference-code/11472/23 "2018-06-07T03:51:10Z")

</div>

> [@ChrisRackauckas](#):
>
> For the last part, have you rebuilt your system image (or compiled Julia from source) and set the optimization level to `-O3` ? Julia won’t use SIMD automatically without that, and that could be the final kicker.

That I’ve definitely not done! I’ve so far played with the JuliaPro and the recently released 0.7, on Mac OS X mainly though I’ve played on Linux too.

Is there a convenient online guide to doing the system image rebuild?

---

<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:** [June 7, 2018, 3:53am UTC](https://discourse.julialang.org/t/understanding-julia-performance-in-simple-finite-difference-code/11472/24 "2018-06-07T03:53:29Z")

</div>

I always end up googling Chris’s blog when I need to remember how to rebuild the system image (see “gotcha #7”): [7 Julia Gotchas and How to Handle Them - Stochastic Lifestyle](http://www.stochasticlifestyle.com/7-julia-gotchas-handle) . Those instructions work for v0.6, but I haven’t tried them in v0.7.

Enabling `O3` is just a matter of starting julia with the `-O3` command-line flag. I’m not sure how to do that in JuliaPro.

---

<div class="post-metadata">

**Author:** ![lwhitefox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lwhitefox/32/4363_2.png) [@lwhitefox](https://discourse.julialang.org/u/lwhitefox)\
**Post date:** [June 7, 2018, 4:03am UTC](https://discourse.julialang.org/t/understanding-julia-performance-in-simple-finite-difference-code/11472/25 "2018-06-07T04:03:53Z")

</div>

I launch Julia 0.7 with -O3 but I don’t get any difference in time.

If I understand the comments from rdeits this should be the same as system image rebuild, but let me know if I’m wrong!

---

<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:** [June 7, 2018, 4:10am UTC](https://discourse.julialang.org/t/understanding-julia-performance-in-simple-finite-difference-code/11472/26 "2018-06-07T04:10:43Z")

</div>

Nope, `-O3` is orthogonal to rebuilding the system image, and you need to do both to turn on every single fancy compiler optimization.

---

<div class="post-metadata">

**Author:** ![lwhitefox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lwhitefox/32/4363_2.png) [@lwhitefox](https://discourse.julialang.org/u/lwhitefox)\
**Post date:** [June 7, 2018, 4:12am UTC](https://discourse.julialang.org/t/understanding-julia-performance-in-simple-finite-difference-code/11472/27 "2018-06-07T04:12:14Z")

</div>

OK, got it - I’ll dig into rebuilding too

---

<div class="post-metadata">

**Author:** ![lwhitefox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lwhitefox/32/4363_2.png) [@lwhitefox](https://discourse.julialang.org/u/lwhitefox)\
**Post date:** [June 7, 2018, 4:16am UTC](https://discourse.julialang.org/t/understanding-julia-performance-in-simple-finite-difference-code/11472/28 "2018-06-07T04:16:43Z")

</div>

Oh, and one last point for the night (my time zone) - I turned off the file writing stuff in the code I posted, and that drops run time down to about 0.2 sec. It is a little hard to make an “apples to apples” comparison with the C code, because it includes some options for various kinds of I/O (it would be a little painful to go through and comment out undesired options). However, for test purposes, I’m running it set up to only print out a single file at the very end, so this is probably a reasonably fair comparison.

So, at this point, it is either a win for Julia or, at worst, a tie! 🎆🎉

Actually, it is a win for Julia either way, since the code is more readable and compact (at least as I start to grasp its nuances).

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [June 7, 2018, 4:50am UTC](https://discourse.julialang.org/t/understanding-julia-performance-in-simple-finite-difference-code/11472/29 "2018-06-07T04:50:27Z")

</div>

> [@rdeits](#):
>
> Enabling `O3` is just a matter of starting julia with the `-O3` command-line flag. I’m not sure how to do that in JuliaPro.

This is at least where it is in Juno

 ![Capture](https://global.discourse-cdn.com/julialang/original/3X/d/e/de0c21d06cf96f8ec8b0ba6b8ccaa117f1c9c342.png)

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [June 7, 2018, 6:13am UTC](https://discourse.julialang.org/t/understanding-julia-performance-in-simple-finite-difference-code/11472/30 "2018-06-07T06:13:43Z")

</div>

> [@rdeits](#):
>
> I always end up googling Chris’s blog when I need to remember how to rebuild the system image

Going to Chris’s blog is always nice, but this particular solution is in the documentation:

[https://docs.julialang.org/en/latest/devdocs/sysimg/#Building-the-Julia-system-image-1](https://docs.julialang.org/en/latest/devdocs/sysimg/#Building-the-Julia-system-image-1)

---

<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:** [June 7, 2018, 6:20am UTC](https://discourse.julialang.org/t/understanding-julia-performance-in-simple-finite-difference-code/11472/31 "2018-06-07T06:20:28Z")

</div>

Forgive me if I’m wrong, but it seems like the purpose of the `nNodes` input argument is to hold the length of the other vectors. That seems like a risky way of implementing it.

Shouldn’t you rather remove that variable and instead have

```julia
nNodes = length(c)

```

in whatever function you need it (or use `end` in the indexing expressions)? Right now it looks really fragile.

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [June 7, 2018, 7:37am UTC](https://discourse.julialang.org/t/understanding-julia-performance-in-simple-finite-difference-code/11472/32 "2018-06-07T07:37:48Z")

</div>

> [@lwhitefox](#):
>
> So, at this point, it is either a win for Julia or, at worst, a tie! 🎆🎉
> 
> Actually, it is a win for Julia either way, since the code is more readable and compact (at least as I start to grasp its nuances).

I see that lately a lot myself, comparing C to Julia.  
I wonder if it’s time to change some of the things in the documentation that say “nearly as fast as C” 😀

---

<div class="post-metadata">

**Author:** ![lwhitefox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lwhitefox/32/4363_2.png) [@lwhitefox](https://discourse.julialang.org/u/lwhitefox)\
**Post date:** [June 7, 2018, 3:28pm UTC](https://discourse.julialang.org/t/understanding-julia-performance-in-simple-finite-difference-code/11472/33 "2018-06-07T15:28:14Z")

</div>

> [@DNF](#):
>
> Forgive me if I’m wrong, but it seems like the purpose of the `nNodes` input argument is to hold the length of the other vectors. That seems like a risky way of implementing it.
> 
> Shouldn’t you rather remove that variable and instead have
> 
> ```julia
> nNodes = length(c)
> 
> ```
> 
> in whatever function you need it (or use `end` in the indexing expressions)? Right now it looks really fragile.

You are certainly correct as the code is right now. In the long run there does need to be a parameter `nNodes` that is a value controlling the set up of the vectors containing material properties. It needs to be set either by the user directly or, possibly, it could be determined based on the number of values in input files too.

However, once that value is set, there are probably places where it would make more sense to use length() - more things to address to finish porting from C!

---

<div class="post-metadata">

**Author:** ![lwhitefox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lwhitefox/32/4363_2.png) [@lwhitefox](https://discourse.julialang.org/u/lwhitefox)\
**Post date:** [June 7, 2018, 4:04pm UTC](https://discourse.julialang.org/t/understanding-julia-performance-in-simple-finite-difference-code/11472/34 "2018-06-07T16:04:12Z")

</div>

For more experienced folks it may be obvious, but it took me a little time to find this settings panel So, for other newcomers, you can get it by selecting the following menu items:

Packages-\>Julia-\>Settings…

Don’t try the general Preferences option 😀

> [@ChrisRackauckas](#):
>
> This is at least where it is in Juno
> 
> [  
> Capture.PNG1918x1080 196 KB  
> ](https://global.discourse-cdn.com/julialang/original/2X/d/de0c21d06cf96f8ec8b0ba6b8ccaa117f1c9c342.PNG)

---

<div class="post-metadata">

**Author:** ![lwhitefox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lwhitefox/32/4363_2.png) [@lwhitefox](https://discourse.julialang.org/u/lwhitefox)\
**Post date:** [June 7, 2018, 4:40pm UTC](https://discourse.julialang.org/t/understanding-julia-performance-in-simple-finite-difference-code/11472/35 "2018-06-07T16:40:58Z")

</div>

> [@rdeits](#):
>
> I always end up googling Chris’s blog when I need to remember how to rebuild the system image (see “gotcha #7”): [7 Julia Gotchas and How to Handle Them - Stochastic Lifestyle](http://www.stochasticlifestyle.com/7-julia-gotchas-handle) . Those instructions work for v0.6, but I haven’t tried them in v0.7.
> 
> Enabling `O3` is just a matter of starting julia with the `-O3` command-line flag. I’m not sure how to do that in JuliaPro.

I’d **highly** recommend that blog post to anyone coming to Julia from a C/C++ like background (and Mathematica). The discussion of arrays in terms of pointers helps me to get a much better grasp of why changes in my code made such a difference.

Same thing with discussion of REPL and globals, as that was a key piece I’d missed before.

---

<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:** [June 7, 2018, 4:45pm UTC](https://discourse.julialang.org/t/understanding-julia-performance-in-simple-finite-difference-code/11472/36 "2018-06-07T16:45:17Z")

</div>

I’ve been playing around with gcc, but for the life of me I can’t get simple programs to run as fast as in Julia.  
Gcc seems relatively (extremely) hesitant to use 256-wide instructions and prefers 128. It’s thus often 50-100% slower than Julia on my computer.  
And when coerced into using 256-wide via `-mprefer-vector-width=256`, it suddenly gets even slower.  
There’s probably something I’m missing, so I asked about it on the gcc help mailing list:  
[https://gcc.gnu.org/ml/gcc-help/2018-06/msg00018.html](https://gcc.gnu.org/ml/gcc-help/2018-06/msg00018.html)

(Disclaimer: I haven’t tried to reproduce this on different computers.)

Overall, my impression is that it’s way easier for non-computer scientists to get blistering fast Julia code than it is C/C++/Fortran!  
Although, I’d be willing to accept Julia being several times slower just for the sake of ease of use…

EDIT:  
On the recent hackernews post by oxinabox, I saw someone say that Julia is probably much slower than advertised in practice, citing lots of StackOverflow posts and the infamous “Giving up on Julia”.  
I think there’re just a lot less people moving from Julia to other languages. Probably because of my inexperience, C/C++/Fortran are all in all failing to live up to the “as fast as Julia!” I’ve implicitly been promised.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [June 7, 2018, 5:17pm UTC](https://discourse.julialang.org/t/understanding-julia-performance-in-simple-finite-difference-code/11472/37 "2018-06-07T17:17:21Z")

</div>

Any interest in writing a blog post to that effect? Maybe with a click-bait title :troll:

---

<div class="post-metadata">

**Author:** ![lwhitefox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lwhitefox/32/4363_2.png) [@lwhitefox](https://discourse.julialang.org/u/lwhitefox)\
**Post date:** [June 7, 2018, 7:27pm UTC](https://discourse.julialang.org/t/understanding-julia-performance-in-simple-finite-difference-code/11472/38 "2018-06-07T19:27:03Z")

</div>

> [@Tamas\_Papp](#):
>
> > [@rdeits](#):
> >
> > I always end up googling Chris’s blog when I need to remember how to rebuild the system image
> 
> Going to Chris’s blog is always nice, but this particular solution is in the documentation:
> 
> [https://docs.julialang.org/en/latest/devdocs/sysimg/#Building-the-Julia-system-image-1](https://docs.julialang.org/en/latest/devdocs/sysimg/#Building-the-Julia-system-image-1)

Hmmm - I tried first the official directions at [docs.julialang.org](http://docs.julialang.org), and had errors that prevented completion. I then used “Chris’s blog”, and it worked like a charm. This was with JuliaPro-0.6.2.2. I did nothing to try to fix the issues (except using the alternate form of statements from the blog):

Here is the error output using the official instructions:

```julia
julia> include("/Applications/JuliaPro-0.6.2.2.app/Contents/Resources/julia/Contents/Resources/julia/share/julia/build_sysimg.jl")

julia> build_sysimg(sysimg_path=default_sysimg_path(), cpu_target="native", userimg_path=nothing; force=false)
ERROR: MethodError: no method matching build_sysimg(::Void, ::String, ::Void; sysimg_path="/Applications/JuliaPro-0.6.2.2.app/Contents/Resources/julia/Contents/Re
sources/julia/lib/julia/sys", cpu_target="native", userimg_path=nothing, force=false)
Closest candidates are:
  build_sysimg(::Any, ::Any, ::Any; force, debug) at /Applications/JuliaPro-0.6.2.2.app/Contents/Resources/julia/Contents/Resources/julia/share/julia/build_sysimg.jl:28 got unsupported keyword arguments "sysimg_path", "cpu_target", "userimg_path"
  build_sysimg(::Any, ::Any) at /Applications/JuliaPro-0.6.2.2.app/Contents/Resources/julia/Contents/Resources/julia/share/julia/build_sysimg.jl:28 got unsupported keyword arguments "sysimg_path", "cpu_target", "userimg_path", "force"
  build_sysimg(::Any) at /Applications/JuliaPro-0.6.2.2.app/Contents/Resources/julia/Contents/Resources/julia/share/julia/build_sysimg.jl:28 got unsupported keyword arguments "sysimg_path", "cpu_target", "userimg_path", "force"
Stacktrace:
 [1] (::#kw##build_sysimg)(::Array{Any,1}, ::#build_sysimg, ::Void, ::String, ::Void) at ./<missing>:0 (repeats 2 times)
 [2] eval(::Module, ::Any) at ./boot.jl:235
 [3] eval(::Any) at ./boot.jl:234
 [4] macro expansion at /Applications/JuliaPro-0.6.2.2.app/Contents/Resources/pkgs-0.6.2.2/v0.6/Atom/src/repl.jl:117 [inlined]
 [5] anonymous at ./<missing>:?

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [June 7, 2018, 8:28pm UTC](https://discourse.julialang.org/t/understanding-julia-performance-in-simple-finite-difference-code/11472/39 "2018-06-07T20:28:36Z")

</div>

~~I think that the method invocation changed a bit between versions, and I linked the latest one. Try [eg the 0.6.3 version](https://docs.julialang.org/en/v0.6.3/devdocs/sysimg/#System-Image-Building-1).~~ EDIT: nope, it did not change at all, but I think what you have in the documentation is just the argument syntax, not an example invocation.

[Previous page](https://discourse.julialang.org/t/understanding-julia-performance-in-simple-finite-difference-code/11472.md?page=1)
