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.
Python has one of the most vibrant and engaged ones communities. Thousands of open-source libraries, tools, and frameworks were developed by people that truly love the language and contribute to it the best they can. It’s very similar to what we see in the JavaScript community, for example.
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.
Finally, most of the major languages were built with a purpose and, with time, amplified to other fronts, like mobile development. Python hasn’t evolved to embrace the mobile world very well, contrary to Java and JavaScript, for example.
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:
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.
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) |
---|---|---|---|
bedcov_c1_cgr.c | C | 5.2 | 138.4 |
bedcov_nim1_klib.nim | Nim | 16.6 | 248.4 |
bedcov_py1_cgr.py | PyPy | 17332.9 | 1594.3 |
bedcov_py1_cgr.py | Python | >33770.4 | 2317.6 |
Source: https://github.com/lh3/biofast
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 |
Source: https://github.com/kostya/benchmarks
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
server-side
$ npm i --save logrocket // Code: import LogRocket from 'logrocket'; LogRocket.init('app/id');
// Add to your HTML: <script src="https://cdn.lr-ingest.com/LogRocket.min.js"></script> <script>window.LogRocket && window.LogRocket.init('app/id');</script>
Would you be interested in joining LogRocket's developer community?
Join LogRocket’s Content Advisory Board. You’ll help inform the type of content we create and get access to exclusive meetups, social accreditation, and swag.
Sign up nowBuild scalable admin dashboards with Filament and Laravel using Form Builder, Notifications, and Actions for clean, interactive panels.
Break down the parts of a URL and explore APIs for working with them in JavaScript, parsing them, building query strings, checking their validity, etc.
In this guide, explore lazy loading and error loading as two techniques for fetching data in React apps.
Deno is a popular JavaScript runtime, and it recently launched version 2.0 with several new features, bug fixes, and improvements […]
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.