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

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? :slight_smile:

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

3 Likes

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

2 Likes

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

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.

5 Likes

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

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

1 Like

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.

21 Likes

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.

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.

3 Likes

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

3 Likes

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)

10 Likes

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.

4 Likes

4 posts were split to a new topic: Salty response to harmless fun question

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.

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 :laughing: 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…

11 Likes