Eccentric Developments

Raybench - Nim (PLC pt.12)

As it's FAQ page explains, Nim is an imperative programming language, that tries to give the users ultimate power without compromises in runtime efficiency; and it delivers.

Nim is very expressive, letting you use classes, first order functions and operator overloading. It compiles to C and Javascript, but the latter won't support some functionality like file system access.


Go version 0.14.2 was used for running the tests.

Compile time

How fast does the compiler takes to generate the binary.

$ time nim c --boundChecks:off --floatChecks:off --opt:speed -d:release nimrb.nim

real	0m0.623s
user	0m0.576s
sys	0m0.028s

Even though Nim compiles to C and then passes thru GCC, compilation time is very fast.

Running Time

$ time ./nimrb

real    1m53.320s
user    1m52.236s
sys     0m0.144s

Impressive!, running times are comparable to C, even a bit faster.

Just for the record, without any changes to the source code, the C implementation can beat this time when using the -Ofast compiler flag.

Code Metrics

Line count: 132 code, 45 blank, 177 total. File size: 5138 bytes.

Less than half the line count of C, and almost reaches the compactness of OCaml. File size-wise, this implementation is less than 300 bytes behind OCaml.

General Thoughts

I like this language, it offers some very flexible string interpolation options, and the compiler is very efficient. It even gives you hints on unused variables and functions.

It supports type inference, similar to OCaml, for tuples. You can create new tuples whenever you want, but if you try to pass it to a function or assign it to a typed variable, it will complain if members are missing (unlike Go).

One very cool feature is that you can write operator overloading; that helps a lot with expressiveness. The language also has many ways for calling functions, and supports polymorphism.

Code blocks are delimited by indentation, but unlike python, vim (using the nim.vim extension) behaved very well.

One thing I didn't like, was having to cast and/or specify the precision of every float number declaration, which is a bit verbose (float32() for casting and 'f32 for literals). I tried avoiding those declarations, defaulting to float64, but the code would run slower.

Anyway, Nim is a great language, and I am very impressed with the expressiveness and more than anything else, it's speed. Too bad it hasn't reached v1.0 yet, as some things are prone to change, or are still in a experimental state.


You can follow the development of this project on GitHub: