The development of new programming languages, in contrast to what we had in the past, happens at light speed nowadays. It’s hard to keep the pace.
At first sight, Nim appears to be another language among the myriad of options out there, but it’s not.
According to the official docs, “Nim is a statically typed compiled systems programming language”. More than that, Nim also was born by the combination of successful features from Python, Ada, and Modula.
Chances are that the last two of them are not known by most of you, but Python is a well-established king in the realm of scripting. So, is it possible that Nim came to become the new substitute for Python? Or do both languages target different audiences?
This article aims to approach those questions through a deeper understanding of the pros and cons of the two languages, as well as what makes each one special. Let’s dive into it!
Created in the 80s by Dutch programmer Guido van Rossum, and written in C (what isn’t), Python was born to embrace the world of scripting.
Massively adopted by universities, scientists, and shell developers in the early days, Python brought a new concept of simplicity to a world filled with languages such as Pascal, C, C++, and Lisp.
Its success was so huge that Python is today one of the most used and beloved languages among all.
Take a look at its growth compared to other very popular programming languages, according to Stack Overflow:
Source: The Incredible Growth of Python
Let’s analyze, then, some of the main reasons that Python is so adored.
Among many factors, perhaps the most important is its versatility. Python is easy to use, easy to read, and very fast to code. And it’s cross-platform!
At the time of its creation, a simple Hello World program would already signal its simplicity, as this comparison between Python and Pascal does:
// Hello World in Pascal program Hello; begin writeln ('Hello, world.'); end. // Hello World Python print("Hello, world.")
Obviously, this wouldn’t be enough to describe all the mechanisms that make the language so easy — it’s more of a starting point.
As a matter of comparison, Stack Overflow counts, by the time of this writing, a total of 1.5 million questions about Python on its forum. Compared to Nim, it is a god, since the number of questions about Nim-lang is just 400.
By far, Python is the giant in this dispute.
Last year, the famous GitHub Octoverse (a compilation of statistics about GitHub’s repositories released every year) ranked Python as the second most used language by the developers of the platform, bypassing Java for the first time:
Source: Octoverse Top Languages.
When it comes to the data science and machine learning areas, Python is way ahead of its competitors, having developed a rich, unique, and complete environment in which anyone with basic programming skills can start from scratch and extract great results in no time.
Among the scientists, it holds a special spot among MATLAB, Scala, and R.
Let’s dive into some other good reasons to pick Python:
Perhaps the most famous downside of the language is its speed. Dozens of studies and measurements were already made to realize that Python is, indeed, slower than a lot of other popular programming languages.
Since it’s an interpreted language, Python has more work to do when reading and “interpreting” the code prior to its transformation into machine code.
Besides that, a classic problem involving threading because of the GIL (Global Interpreter Lock) also features within the list of problems. GIL is a mutex and allows only one thread at a time. As a result, if you aim to load multi-threaded programs so they’re going to run slower than the single-threaded ones.
Android and iOS, with their set of supported languages, don’t understand Python. Even though you have available cross-platform libraries like Kivy, for example, they don’t represent a substantial portion of the global mobile development market.
I’ll leave you with some more popular downsides taken from the community:
Before we dive into the universe of Nim, and since it is a fair new language for most of you, let’s start with a single example including its setup.
There are a handful of options for installing Nim on your computer. Since all the options rely on single commands for each OS, I’m going to leave this task for you. You can find the proper instructions here.
Once the installation is finished, run the
nim -v command to check if everything went well:
For this example, the code samples will be developed under the VS Code IDE. It has a very good extension for Nim that adds language support for the IDE.
Select a folder of your preference and open it within VS Code. Then, create a new file called
hello_world.nim (yes, unsurprisingly, that’s the Nim’s file extension) and place the following code into it:
echo "Who should I greet?" var name: string = readLine(stdin) echo "Hello, ", name, "!"
If you’re familiar with “hello worlds”, this won’t be a problem for you to understand. If you’re familiar with Linux, for example, you already know what the echo command does.
This command is the equivalent version of the Unix-like operating system command that outputs the strings passed as params. Not a big deal.
Right below, we’re reading some input from the console; then, we’re printing again the formatted hello message.
To run the program, go to the command line and issue the following command:
nim compile --run hello_world.nim
With Nim, since it is a compiled language, you need to first compile the code via a compile command and run it. A short version of the same command is also available as:
nim c -r hellow_world.nim
Another interesting thing is the execution output. Before the program starts, you get to see some lines printed to the console:
Take a look at the lines that starts with
std… That’s a sign of the C++ influence.
As opposed to Python’s speed, Nim’s execution is fast. Much faster than Python’s. A good way to explore the performance capabilities of programming languages is towards the execution of benchmark tests.
There are tons of benchmarks available for Python. Our problem is with Nim since it’s way less popular than Python.
Biofast is a small benchmark for evaluating the performance of programming languages and implementations on a few common tasks in the field of Bioinformatics. Its benchmark is basically related to processing big files and running similar algorithms in many different languages.
In the table below, you can find the results for loading BED files (tab-delimited text files) into memory and computing intervals through cgranges algorithms:
|Program||Language||CPU time (s)||Peak memory (Mb)|
I’ve removed some of the table of contents to simplify the results. Have a look over the difference in CPU time and memory consumption from Nim to Python. C is exhibited just for a matter of comparison since it is super fast.
Perhaps the benchmarks developed by kostya are more complete and diversified. We’ve got tests over Base64, JSON parsing, memory allocation, and other famous algorithms. These are the results for the Havlak’s loop finder implementations:
|Language||Time, s||Memory, MiB||Energy, J|
|Nim GCC||12.05 ± 00.06||485.71 ± 09.19||225.42 ± 01.38|
|C++||15.06 ± 00.11||178.24 ± 00.05||235.89 ± 03.36|
|PyPy||30.38 ± 00.50||664.47 ± 82.61||545.93 ± 10.51|
|Python||108.91 ± 00.28||403.83 ± 00.05||1896.41 ± 15.93|
Incredibly, depending on the algorithm nature, Nim’s speed can even beat C++’s. That’s a great representation of how powerful Nim can be to your project’s performance.
When it comes to executables, Nim comes built with the ability to package its executables with the dependencies included within it. That means that, differently from Python, you don’t need to rely on the Python’s installed on that specific machine. It will execute across different Unix-like systems and there’s no need to worry about broken linkages.
Apart from that, let’s bullet-list some other nice advantages:
The biggest cons against Nim is, of course, its popularity. Chances are that you’ve never heard of it before this article.
Nim is older (created in 2008) than other famous languages like Swift (created in 2014), for example. Even though, Swift features > 277k questions on Stack Overflow at the moment.
A quick comparison between Nim and Python on Google Trends reveals an enormous gap:
Because of that, it’s hard to point out the downsides. There’s not much content about it. Besides that, the official documentation is pretty good and very complete.
Regarding the syntax, some developers agree that Nim is easier than C, but a bit verbose when compared to Python.
Some other downsides could be listed as follows:
This article couldn’t be an all-in-one complete piece of discussion about both languages. So, please share your thoughts as well about both languages and the experience you’ve had so far.
Some of Nim developers state that it would be better suited for people that come from a C/C++ background and would like to learn a scripting language. Perhaps the curiosity over Python’s comparison comes from the fact that both languages are a reference in terms of scripting.
Maybe if Nim had come from the official support of one of the tech giants, like Google’s Go, the scenario would be a bit different. However, a language doesn’t have to be big to be loved, strong, and useful. You just need the right amount of curiosity in hand and go to action. Good luck!
Install LogRocket via npm or script tag.
LogRocket.init() must be called client-side, not
Web components are underrated for the performance and ergonomic benefits they provide in vanilla JS. Learn how to nest them in this post.
defer feature, introduced in Angular 17, can help us optimize the delivery of our apps to end users.
ElectricSQL is a cool piece of software with immense potential. It gives developers the ability to build a true local-first application.
Leptos is an amazing Rust web frontend framework that makes it easier to build scalable, performant apps with beautiful, declarative UIs.