Image for post
Image for post
Photo by Chris Ried on Unsplash

Speeding up Python code with Nim

Python is one of the most popular and accessible programming languages of all time but is far from being the fastest. Many library and framework creators resorted to using C extensions to make their code run faster than native Python. Although that works well enough, for someone un-acquainted with C, it’s a hell on earth to manage garbage collection and memory safety. And that’s where Nim comes in.

What is Nim?

It’s a statically typed, compiled, object-oriented programming language. Nim was created to be a language as fast as C, as expressive as Python, and as extensible as Lisp. Due to its syntax similarity to Python, it’s an amazing choice for an extension language when C isn’t your cup of tea.

Getting started with Nim

To get started with writing Nim code, you first have to install it on your system. To do that, head over to nim-lang.org, download, and then install it.

Once you’re done, let’s write your first lines of Nim code. First, we’ll create our file, I’ll name mine hello.nim , you can name yours whatever you want. Now, we’ll open it with any text editor and write the following:

Save and close the file, then open Command Prompt in the file’s location and type:

And voila! “Hello, World!” should have been printed to the console. That’s the basics on Nim for you, now let’s get into the main focus of this article.

Embedding Nim in Python applications

Nim comes preinstalled with a module called nimpy , and a module called nimporter is available for Python. The latter can be installed using pip install nimporter. Those are the two packages that are going to be essential when working with those languages together.

To demonstrate Nim’s capabilities, we’ll create a simple benchmark comparing the speeds of both languages using a naive function finding the nth number of the Fibonacci Sequence.

Let’s create a folder called Benchmark , with 3 files inside:

  • main.py — the file we’ll be running
  • nmath.nim — the file that’ll hold the Nim version of our fib function
  • pmath.py — the file that’ll hold the Python version of our fib function

First, let’s write our fib function in pmath.py

Let’s move to nmath.nim . First off, we have to import nimpy like this:

Just like Python, right? Now, for the function itself:

Let’s break this down.

We define a function fib using the proc keyword. We specify the return type as an integer, and Woah! What’s that monstrosity? {.exportpy.} signals Nim that this function is meant to be used in another, Python module. Other than that, everything is exactly the same as in Python.

Benchmarking

In main.py we’re going to create a simple benchmark:

And that’s it!

The nimporter package resolves Nim imports into regular Python modules to be used just like native ones. Cool right?

To run the code, type the usual python main.py command into Command Prompt and observe!

Summary

And that’s it for the quick rundown of embedding Nim in Python. Nim came out to be about 30x times faster, although keep in mind that differences in speed may vary depending on the task being done. To get to know Nim better, I highly suggest looking through the official documentation and reading up on Wikipedia.

Alright, this is all for the tutorial! Thanks, for bearing with me until the end. If you found the article helpful, please consider leaving some claps!

Hi! I’m wulf. A software developer, basketball player, and an avid dog lover. Always looking to meet new people and learn new things.

Sign up for statch

By statch

The statch newsletter. Read about tech-related topics that actually interest you. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Get the Medium app