Skip to content

Performance

litlighilit edited this page Jan 1, 2026 · 1 revision

NOTE: The following was written when NPython was in v0.1.0, statistics may change with version increasing.

Performance

Nim is claimed to be as fast as C, and indeed it is. According to some primitive micro benchmarks (spin.py and f_spin.py in tests/benchmark/), although NPython is currently 5x-10x slower than CPython 3.7, it is at least in some cases faster than CPython < 2.4. This is already a huge achievement considering the numerous optimizations out there in the CPython codebase and NPython is focused on quick prototyping and lefts many rooms for optimization. For comparison, RustPython0.0.1 is 100x slower than CPython3.7 and uses 10x more memory.

Currently, the performance bottlenecks are object allocation, seq accessing (compared with CPython direct memory accessing). The object allocation and seq accessing issue are basically impossible to solve unless we do GC on our own just like CPython.

Drawbacks

NPython aims for both C and JavaScript targets, so it's hard (if not impossible) to perform low-level address based optimization.

Nim 0.x GC

NPython relies on Nim GC. Frankly speaking, in the past, it was not satisfactory.

  • The GC used thread-local heap, which once made threading once nearly impossible (for Python), though not so for Nimv1 and Nimv2.
  • The GC could hardly be shared between different dynamic libs, which meant NPython can not import extensions written in Nim.

If memory was managed manually, these drawbacks could be overcomed early.

Nim v1 and v2 MM

However, in current years, Nim, specially v2, has improved a lot on GC, which's now called MM(Memory Management).

And Nimv2 uses ORC by default, which offers deterministic performance and uses a shared heap.

Not only has threading programming been enhanced and become easy to write, but also setupForeignThreadGc() and tearDownForeignThreadGc() come out here for foreignal call to control Nim's MM.

In short those difficulties that once held us back have disappeared.

Clone this wiki locally