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:
- Do you want to go for a Procedural programming style? Go ahead. Prefer Object-oriented? Functional programming? That’s ok too. Python embraces them all
- The number of available jobs is huge, with salary averages reaching 100k (in the US)
- A great choice for game development, with tons of nice tools like pygame (a set of Python modules for writing games)
- The favorite bet for teaching kids how to program. Through graphic tools like Turtle, things can become more visual
- Last but not least, Python is very well documented. Go ahead, have a good read
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.
However, community efforts over projects like pydroid and qpython put more hope in the future of Python mobile development.
I’ll leave you with some more popular downsides taken from the community:
- Python is a bit more complex in terms of shipping and distributing its software
- Since a big part of the projects and, therefore, the developers still use Python 2, we get to deal with a big problem of legacy codebases that probably won’t be upgraded at any time soon. The syntactical differences between versions 2 and 3 are big, but that’s okay. That’s why we have versioning, in the first place. However, this fragmentation inheritance from version 2 turns things slightly difficult
- A lot of people still complain about Python’s support for Functional Programming. Although the language comprises what involves higher-order functions and list comprehensions, there are pitfalls around features like tail calls and lambdas, among others
- The design of the language, which puts the indentation as the factor to define the code block’s formatting, is considered a downside by many. If you come from other traditional languages, it can be hard to adapt to it and tricky to code
Let’s get to know Nim
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.
Set up & example
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.
However, we can find two great benchmarks today made by the community that can give us some insights over the performance differences between both languages: the Biofast and Kostya’s benchmarks.
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:
- It also supports a nice feature: macros. It’s very useful for machine learning development
- It has a great metaprogramming design. We have generics, templates, and the macros we just mentioned
- Its typing is strict, which means that Nim checks your code when compiling it
- It is also multi-paradigm, embracing OOP, Functional and Imperative Programming
- Provides smooth async support that comes built-in
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:
- It wouldn’t be a good choice of language to find a job, at all. Unless you’re working for your own project or some sort of scientific gigs
- Although the GitHub issues are responded, they have today 1.7k open issues against 1.3k from Python. Python, however, seems to have more interactions going on, as well as contributors (1310 against 551 for Nim)
- There are not as many big projects made under Nim as we have with Python. That could be tricky if your project is critical and a major factor in helping you decide between the two
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!
Get set up with LogRocket's modern error tracking in minutes:
- Visit https://logrocket.com/signup/ to get an app ID
- Install LogRocket via npm or script tag.
LogRocket.init()must be called client-side, not server-side
- (Optional) Install plugins for deeper integrations with your stack:
- Redux middleware
- NgRx middleware
- Vuex plugin
$ npm i --save logrocket
import LogRocket from 'logrocket';
Add to your HTML:
<script>window.LogRocket && window.LogRocket.init('app/id');</script>
2 Replies to “Nim vs Python: Which should you choose?”
* the first benchmark you’re referring to isn’t using the most optimized compiler flags for nim, see https://github.com/lh3/biofast/issues/21#issuecomment-707979684 where you can get easily a 1.5x speedup by using `-d:danger –gc:arc`
* worth mentioning is you can embed a python session using nim (or vice versa) via https://github.com/yglukhov/nimpy so your can integrate either way
* see also https://github.com/timotheecour/D_vs_nim for a D vs nim comparison
* not mentioned in this article is that nim compile times are far better than typical equivalent idiomatic code in C or in particular C++
Hello, nice article. I have a question, after learning a little bit of Go I can see that concurrency is super easy and powerful. What’s your opinion about Nim’s concurrency.