r/nim • • Jan 08 '22

Nim's while loops 7x slower than Python?

I found something strange when messing around with while loops, my Python program can do about 7x the iterations of Nim in 1 second:

Python 3.10.1

import time

currentTime = time.time()
interval = 1
count = 0

while time.time() - currentTime < interval:
    count += 1

print(count)

Output: 5 513 863

Nim 1.6.2

import std/times

let currentTime = now()
let interval = initDuration(seconds = 1)
var count: int = 0

while now() - currentTime < interval:
    count += 1

echo count

Output: 799 346

C++ (gcc 11.2.0)

#include <iostream>

int main() {
    auto currentTime{time(NULL)};
    int count{};
    int interval{1};

    while (time(NULL) - currentTime < interval) 
        count++;

    std::cout << count;
}

Output: 412 992 387

Am I doing something wrong?

2 Upvotes

10 comments sorted by

15

u/haxscramper Jan 08 '22

std/times.now() returns DateTime object which you then subtract from, then compare. The object itself is quite big https://nim-lang.org/0.20.0/times.html#DateTime and surely has much higher overhead compared to the time() function from C that returns integer. I would imagine python time handling logic has a lower overhead as well.

So in short, you are benchmarking time object handling speed, and not the loop itself. count++ overhead is negligible in this benchmark.

You need to exclude anything besides count increments and integer compares from the loop - I would imagine time()/now()/time.time() does a lot of system calls etc. Instead, compare how long does it take to count to a billion, for example.

7

u/[deleted] Jan 08 '22 edited Jan 08 '22

My bad, you're right! Python about 50% 50x slower now.

5

u/padraig_oh Jan 08 '22

just 50%?

i compared c/c++/nim/py3 with this code:

//test.c
int main(){
	int a=0;
	while(a<1000000000)a++;
	return 0;
}

//test.cpp
int main(){
	int a=0;
	while(a<1000000000)a++;
	return 0;
}

//test.nim
var a=0
while a<1000000000:
    a+=1

//test.py
a=0
while a<1000000000:
    a+=1

and then compared the time to run on linux with $ time prog where prog is python3 test.py etc. (note that with the exception of python, the programs are pre-compiled.) and the results are as follows:

$ time python3 test.py
real	0m55,148s
user	0m55,075s
sys	0m0,007s

$ clang test.c -o test_c
$ time ./time_c
real	0m0,807s
user	0m0,806s
sys	0m0,000s

$ clang++ test.cpp -o test_cpp
$ time ./test_cpp
real	0m0,489s
user	0m0,488s
sys	0m0,000s

$ nim c -o:test_nim test.nim
$ time ./test_nim
real	0m3,803s
user	0m3,796s
sys	0m0,000s

$ time nim c -r test.nim # for comparison
real	0m4,308s
user	0m4,298s
sys	0m0,054s

not quite sure how you arrived at just twice the time for python, this is very surprising. cpu will of course make a difference, but that is still weird..

sidenote: if you optimize any of these programs (except python) the compiler will evaluate the loop at compile-time and thereby reduce the runtime to basically zero

3

u/[deleted] Jan 08 '22

*50x slower not 50%

5

u/padraig_oh Jan 08 '22

Ah, well. That's one hell of a typo

0

u/[deleted] Jan 11 '22

[deleted]

3

u/padraig_oh Jan 11 '22

see my sidenote about that. if you turn on optimizations, the loops are evaluated at compiletime, not runtime, so you are not comparing loops anymore, you compare compiler optimizations.

8

u/[deleted] Jan 08 '22

First of all, you're not actually comparing the performance of while loops - you're comparing the performance of time functions.

That said, try compiling the Nim version with -d:release or even -d:danger if you haven't already - even the compiler tells you that the debug build is slow (unless you disabled hints).

But yeah, this is not representative of real world performance at all, Nim while loops are the same "speed" as C/C++ if you really want to compare their performance.

3

u/TheDarkMode Jan 08 '22

There is a reason why they say don't benchmark small samples, and I think you found out why

2

u/TheDarkMode Jan 08 '22

Follow Up Question:

Since Nim is compiled to C, I would of thought it's time module, would of been based off C. Yet in the above example the Cpp is still faster then the Nim implementation, is that because Nim has extra functionality and it is a trade off from Speed vs Usability?

Or is this a use case where using Cpp is better?

Food for thought

1

u/Familiar_Ad_8919 Jan 08 '22

try

powershell -Command "Measure-Command {path\to\your\file.exe}"

it gives a more realistic measure