GO / RUST / CPP – Julia
If you have reached this post, perhaps you have been also wondering what are the benefits and drawbacks of the mentioned languages and which one to choose for your project or career. I have decided to create this post specifically to discuss some key differences between those languages. For that, I have decided to test them a little bit with one my favorite demos – computing Julia Fractals. The first thing about learning and testing something new: Always do it yourself the way you want and include some nice colors! 😇
I will skip the obvious things regarding Rust/ GO, which are basically the native support for parallelism and memory safety. Indeed, C++ is among the languages most prone to human errors and especially in regards to the memory safety. GO and Rust deal with this problem in different ways, but both guarantee memory safety. Both the GO and Rust languages also comes with a build-in management system of external libraries, which is great benefit, but on the other side, even CPP is not that much far behind with the new C++20 Modules.
After working with rust for a few months, I have to say that the package management system (cargo) works, but there are some drawbacks to this as well. Most of the time, you will see suggestions such as : “Oh, just use this or that cargo“, which may sound great and simplify things, but you can easily end up having dependencies on ~100 of external packages. I am actually very skeptic about this and personally believe that this is a potential problem – nobody actually knows what are the dependencies, how stable they are and if they are going to be supported at least for some time.
Indeed Rust is among the “Newer” languages, which brings also some interesting topics such as: bad or outdated documentation in the first place, but closely behind in the second case also the frequency of changes among the packages. If you would like to stay using always the latest version, then you will most likely end up doing adjustments to you code quite often.
I have been using C++ for the past years and despite some drawbacks (probably only the missing package management), I would say that its a great language. It allows you to literally do whatever you want and the highest speed. On the other hand, I have seen C/C++ codes, that were so bad, that its really beyond your imagination. That being said, if you dont have experience with C++, it really easy to make tremendous errors. Experienced C++ programmers on the other hand tend to create code, which is also not very readable and if you want to obfuscate your code in C++, your hands are also completely free. Here comes some of the benefits of the GO / Rust languages, which enforces the programmer to use at least some coding guidelines and styles. Go being less strict here. Seriously, Rust’s snake_case really pisses me off so much!
Anway, lets have a look at the major differences and what is the major idea behind the programming languages:
- C++ : Gives you the complete freedom to do whatever you want, you are the master of whatever happens right and wrong in your application.
- GO : Easily solve complex problems with less effort at great speed while still not having to bother with memory safety.
- RUST: Forces you to write safe code by definition.
That being said, lets at it more closely: In Go, you are still allowed to write code, that is potentially unsafe in terms of parallelism, so its still the programmer’s responsibility to make sure that he or she knows what they are doing. In RUST, you simply can’t and the compiler will not allow you to do it even if you want and know that its safe to do it. That is probably the source of the problem – If you love freedom as much as I do, then perhaps you will really hate when somebody forces you to obey some protocols, which you know are 100% useless.
In other words: The Julia example – I wrote the entire application in just a few hours in GO and mostly due to the fact, that I am not an experienced GO programmer and dont know the correct syntax. C++ took me even longer mostly because I was tackling some issues with Boost / OpenCV libraries in MS visual studio. What took me however most of the time was the RUST programming language (I finally used the Rayon’s scoped threads).
The Julia example might be parallelized in a variety of ways. I wanted each thread to compute a piece of the final image. And that turned out to be a huge problem in RUST! – Huge unless you just use a cargo (namely Rayon / scoped thread pool etc …). I wanted to basically do the same as in C++. That is to create a threadpool, assign work for each thread and wait for execution completion (As simple as that!). I really wasn’t able to do it even after spending days of research of how to properly distribute the work among the threads. Thats where I probably came to a conclusion, that if the language does not allow you to do a simple thing like this – then its probably not worth your time.
The problem with Rust is that poeple, who already knows how to guarantee memory / thread safety dont need to code in Rust to get these benefits and people, who would appreciate the benefits would find it extremely difficult to code in Rust unless they “Use 3rd party cargo for everything”. Rust still has its benefits and its ultra fast-calling FFI (Foreign Function Interface) makes it even a good candidate if extensive cooperation between C++ is required, but for myself personally, its really too strict in everything. Go provides the reasonable compromise between all those things: It gives you benefits of freedom, memory safety and easy parallelism as awell as package management at a cost of slighly worse performance. Since time’s really expensive, I believe that GO simply offers the best combination of all of what can be understood by a “Modern Computer Language” 🦫Its Google’support and widely usage makes it also a reliable candidate (RUST was backed by Mozilla until COVID – Era) especially for cloud-computing.
The idea behind GO is to allow for fast and safe development of complex applications with minimum knowledge of that language. This is something that no other language offers. Go brings the benefits of modern language at a cost of reduced performance. That being said – the performance is still exceptional. I would highly recommend it for both beginners and even experienced programmers.
Result for computing Julia in resolution [15360 x 8640] (i7-12700H – 20 Threads) :
C++ : ~610 ms || RUST : ~850 ms || GO : ~1230 ms
All Go / Rust / C++ files are available➡️HERE⬅️
NOTE: You will have to download and Configure your C++ Project with Boost 1.83 and OpenCV 4.81