# A bet: what specific algorithms in Julia can be faster or as fast as C++ implementations?

**URL:** <https://discourse.julialang.org/t/a-bet-what-specific-algorithms-in-julia-can-be-faster-or-as-fast-as-c-implementations/13564>\
**Category:** Performance\
**Created:** [August 16, 2018, 12:12pm UTC](https://discourse.julialang.org/t/a-bet-what-specific-algorithms-in-julia-can-be-faster-or-as-fast-as-c-implementations/13564 "2018-08-16T12:12:16Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![sairus7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sairus7/32/10816_2.png) [@sairus7](https://discourse.julialang.org/u/sairus7)\
**Post date:** [August 16, 2018, 12:12pm UTC](https://discourse.julialang.org/t/a-bet-what-specific-algorithms-in-julia-can-be-faster-or-as-fast-as-c-implementations/13564/1 "2018-08-16T12:12:16Z")

</div>

Me and my colleague have made a bet that Julia can be faster than C++ in several test cases. I will write a set of algorithms in Julia, and he will write the same algorithms in C++, then we will measure the performance.

My question is: what specific\* algorithms should I choose to win? 🙂

\*Our work is related to medical devices and signal processing.

---

<div class="post-metadata">

**Author:** ![lobingera](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lobingera/32/211_2.png) [@lobingera](https://discourse.julialang.org/u/lobingera)\
**Post date:** [August 16, 2018, 12:27pm UTC](https://discourse.julialang.org/t/a-bet-what-specific-algorithms-in-julia-can-be-faster-or-as-fast-as-c-implementations/13564/2 "2018-08-16T12:27:21Z")

</div>

“Benchmarks don’t lie, but liars do benchmarks.”

---

<div class="post-metadata">

**Author:** ![sairus7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sairus7/32/10816_2.png) [@sairus7](https://discourse.julialang.org/u/sairus7)\
**Post date:** [August 16, 2018, 12:37pm UTC](https://discourse.julialang.org/t/a-bet-what-specific-algorithms-in-julia-can-be-faster-or-as-fast-as-c-implementations/13564/3 "2018-08-16T12:37:31Z")

</div>

I’m afraid you misunderstood the purpose of this bet. 😉

---

<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:** [August 16, 2018, 12:40pm UTC](https://discourse.julialang.org/t/a-bet-what-specific-algorithms-in-julia-can-be-faster-or-as-fast-as-c-implementations/13564/4 "2018-08-16T12:40:41Z")

</div>

Something that vectorizes in Julia with `@simd` but has a hard time vectorizing without that.

Could also try do something that benefits of explicit simd and use SIMD.jl. Hopefully, your opponent won’t resort to simd intrinsics him/herself.

---

<div class="post-metadata">

**Author:** ![m4r35n357](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/m4r35n357/32/4848_2.png) [@m4r35n357](https://discourse.julialang.org/u/m4r35n357)\
**Post date:** [August 16, 2018, 1:06pm UTC](https://discourse.julialang.org/t/a-bet-what-specific-algorithms-in-julia-can-be-faster-or-as-fast-as-c-implementations/13564/5 "2018-08-16T13:06:31Z")

</div>

My advice to you would be to steer well clear of arbitrary precision 😉

(at least if you want this to be a beauty contest)

---

<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:** [August 16, 2018, 1:15pm UTC](https://discourse.julialang.org/t/a-bet-what-specific-algorithms-in-julia-can-be-faster-or-as-fast-as-c-implementations/13564/6 "2018-08-16T13:15:36Z")

</div>

A deep neural net with a diffeq layer on 128-bit floats with confidence intervals embedded. Julia would be faster by default since your opponent wouldn’t be finished coding, so you can grab a beer after finishing in 15 minutes and wait for him to give up.

---

<div class="post-metadata">

**Author:** ![tomaklutfu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomaklutfu/32/2411_2.png) [@tomaklutfu](https://discourse.julialang.org/u/tomaklutfu)\
**Post date:** [August 16, 2018, 1:23pm UTC](https://discourse.julialang.org/t/a-bet-what-specific-algorithms-in-julia-can-be-faster-or-as-fast-as-c-implementations/13564/7 "2018-08-16T13:23:29Z")

</div>

I think julia`s benefit comes from being a high level language that enormusly helps expressing rather difficult things in C++ very easily at the meantime having comparable performance.

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [August 16, 2018, 1:42pm UTC](https://discourse.julialang.org/t/a-bet-what-specific-algorithms-in-julia-can-be-faster-or-as-fast-as-c-implementations/13564/8 "2018-08-16T13:42:59Z")

</div>

Any heavy numeric code that fulfills these two conditions can easily beat its C++ equivalent:

1. properly gets SIMD vectorized, check with `@code_llvm` and look for instructions like `4 x double`
2. avoids bounds checking, by `--check-bounds=no` compiler option, also try `--inline=yes`

These are the two major tricks that `ifort`, e.g., does to beat all competitors. If you have arrays, use `StaticArrays` or `tuples`, these are immutable. Finally avoid input/output, strings, and parsing files, Julia still is not as good in these.

---

<div class="post-metadata">

**Author:** ![thofma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thofma/32/1691_2.png) [@thofma](https://discourse.julialang.org/u/thofma)\
**Post date:** [August 16, 2018, 1:49pm UTC](https://discourse.julialang.org/t/a-bet-what-specific-algorithms-in-julia-can-be-faster-or-as-fast-as-c-implementations/13564/9 "2018-08-16T13:49:04Z")

</div>

Although it does not answer your question: To make it more interesting, you should not only measure running time, but also development time.

---

<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:** [August 16, 2018, 1:53pm UTC](https://discourse.julialang.org/t/a-bet-what-specific-algorithms-in-julia-can-be-faster-or-as-fast-as-c-implementations/13564/10 "2018-08-16T13:53:50Z")

</div>

Try something that involves evaluating a complex-valued polynomial, e.g. a power-series approximation to a special function. Your friend will be hard-pressed to beat the code generation of the `@evalpoly` macro for complex arguments, though of course it is theoretically possible to implement this in C++.

(Here is an example that combines `@evalpoly` with symbolic manipulation of continued-fraction expansions in a macro, making it especially easy to beat C++ programs for continued-fraction evaluation: [https://github.com/stevengj/18S096/blob/iap2017/pset2/pset2-solutions.ipynb](https://github.com/stevengj/18S096/blob/iap2017/pset2/pset2-solutions.ipynb))

---

<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:** [August 16, 2018, 2:06pm UTC](https://discourse.julialang.org/t/a-bet-what-specific-algorithms-in-julia-can-be-faster-or-as-fast-as-c-implementations/13564/11 "2018-08-16T14:06:15Z")

</div>

> [@stevengj](#):
>
> Try something that involves evaluating a complex-valued polynomial, e.g. a power-series approximation to a special function. Your friend will be hard-pressed to beat the code generation of the `@evalpoly` macro for complex arguments, though of course it is theoretically possible to implement this in C++.
> 
> (Here is an example that combines `@evalpoly` with symbolic manipulation of continued-fraction expansions in a macro, making it especially easy to beat C++ programs for continued-fraction evaluation: [https://github.com/stevengj/18S096/blob/iap2017/pset2/pset2-solutions.ipynb](https://github.com/stevengj/18S096/blob/iap2017/pset2/pset2-solutions.ipynb))

I think we’re on the same page here. Someone can definitely match Julia with C++ on any algorithm. The issue is scaling the good algorithms to larger and larger (or more realistic) algorithms. @stevengj’s example is a quick way to do something that, with enough gusto you can get a solution in C++ and match Julia, but the obvious solution is slow and bad. Once your C++ opponent catches up, just scale the amount of the type system and code generation you use and go take a nap.

Julia code complexity scales like `O(sqrt(n))` while C++ code complexity scales like `O(n^3)` (very similar to matrices) where `n` is the number of lines of code you have to maintain (and there’s an extra effect when build systems get involved). I wouldn’t say C++ can’t do something: just make the point that you don’t want to be the one writing said C++ code.

---

<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:** [August 16, 2018, 4:10pm UTC](https://discourse.julialang.org/t/a-bet-what-specific-algorithms-in-julia-can-be-faster-or-as-fast-as-c-implementations/13564/12 "2018-08-16T16:10:06Z")

</div>

4 posts were split to a new topic: [Salty response to harmless fun question](https://discourse.julialang.org/t/salty-response-to-harmless-fun-question/13577)

---

<div class="post-metadata">

**Author:** ![sairus7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sairus7/32/10816_2.png) [@sairus7](https://discourse.julialang.org/u/sairus7)\
**Post date:** [August 16, 2018, 3:36pm UTC](https://discourse.julialang.org/t/a-bet-what-specific-algorithms-in-julia-can-be-faster-or-as-fast-as-c-implementations/13564/15 "2018-08-16T15:36:46Z")

</div>

Ok, I will explain: the purpose is to demonstrate Julia usability and limitations in a concrete context.

I agree, there are other criteria to estimate the usability of Julia for big and complex projects of production-quality. If you know some tutorials or examples of how-to build such projects with Julia, please point me there.

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [August 16, 2018, 4:22pm UTC](https://discourse.julialang.org/t/a-bet-what-specific-algorithms-in-julia-can-be-faster-or-as-fast-as-c-implementations/13564/16 "2018-08-16T16:22:31Z")

</div>

I had some luck in a previous bet with an office mate who was writing some evolutionary algorithm in Matlab. It was a relatively small program, had some nice loops and `getindex` operations, if-statements introducing branches which can be easily replaced with the branch-free `ifelse` function. So I accepted the challenge. Half an hour later, bam my program is 8x faster than his Matlab version to the point that he thought I was cheating and using multi-threading 😆 I told him I can probably make it 3-8 x faster still if I use multi-threading; the code had some embarrassingly parallel loops. He may not have optimized his Matlab code as much as I optimized my Julia code, but he did take more than half an hour figuring out vectorization tricks, so point made…
