Cover image by Markus Krisetya on Unsplash
During the weekend and out of curiosity after watching this video on YouTube, I thought of doing a similar experiment of comparing the execution speed of different programming languages which runs a loop from 0 to 1 billion.
So, I wrote a similar program in some of the popular programming languages and timed the execution of the program. Each program contains a loop from 0 to 1 billion. The following table contains the result of the experiment.
Language | Time taken (in seconds) |
---|---|
Node.js | 1.68s |
Golang | 2.5s |
C# | 3.05s |
C++ | 3.49s |
Rust | 25.18s |
PHP | 25.31s |
Ruby | 128.17s |
Python | 159s |
I have used the Windows PowerShell Measure-Command
command to time the execution of the program.
I wrote a program something similar to the below in different programming languages.
count = 0
for i in range(1000000000):
count += 1
I have used the for
loop in all the programming languages.
Note: I have executed all the programs in debug mode. The results in release mode (with optimization) may be different.
System configurations
Below are the details of the computer I have used for the experiment-
Processor - Intel Core i3-6006U CPU @ 2.00 GHz | 2 Cores
RAM - 8 GB
Operating System - Windows 10
Conclusion
I was expecting C++ and Rust to top the table but surprisingly Node.js beats them all. And Rust is not even close to C++. Python is the worst performer. I don't know the exact reason for this behavior but it might be related to the architecture of the languages.
P.S. - I have done this experiment just for out of curiosity and it is not intended to show if a programming language is better than the other. Every language is designed for the specific use-cases. And I love multiple programming languages too.
Feel free to share the result of other programming languages which I didn't tested for.
Top comments (26)
I agree with @superfola, I believe we need more insight on your methodology.
On a different hardware I got results similar to yours only for Node.Js and PHP 8.1, while Rust was totally different.
This little guy got 26 seconds to run:
Node 18. performed slightly better on my HW:
While Rust took almost no time:
With debug symbols the situation changes perceptibly:
Obviously these are not proper benchmarks but does help us understand the magnitude of execution time.
I suspect you did count the build time of cargo and the runtime of the program, not the actual program running, but we need more info to be sure.
Possibly the same with C++.
I had built the code using
rustc
. I have tried using thecargo build --release
and it took no time. Thanks.please edit your post then, it can lead to confusion about Rust performance
Interesting! I suspect the JIT compiled languages here (C#, Node.js) are optimising out the loop since it doesn't do anything, as would C++ and probably other AOT compilers if told to optimise (eg: -O3) If you called say a random number generator, that the optimiser cannot predict (eg: rand() in C++) on each iteration, you might find things are very different; performance measurement is hard 😄
Calling a random number generator would obscure the results a lot, unless all of the languages compared used the same algorithm for pseudorandom numbers.
Adding a statement after the loop to print the value of count might help. That should prevent optimization from saying "we're not doing anything with count so we don't need this loop."
Yes that may be the reason for the performance.
Your results might be like this because of the type of builds you produced. Since you are measure a quite trivial operation (an addition), I would have expected C++ to be on top, with Rust alongside.
Did you compile in release mode?
No, I compiled all in Debug mode. I just tried C++ in release mode and it executes faster (~0.1s).
I would be interested to see a another test done using the
count
variable as a reference type and having to do heap access for each iteration. There should be decent resolution using a fraction of the iterations to see the difference (something like 10 million might be enough), but this would be telling as well for each language. I'm not set up for all of these languages or I would do this myself.That's because python's autour has always prioritized people's needs over micro benchmarks and I think that's the right trade off.
People who want to do C++ style super low level stuff already have C++
Rust is very slow when it's in dev mode because of all its extra features. Those are removed in production and executes extremely fast. Perhaps you should get a benchmark of that in prod mode?
(Btw, I was really surprised that NodeJS made the top. I tried some of those myself and you were surprisingly correct!)
Hmmmm, I got quite different results on the M1 Pro. All tests fell withing the 1-4 secs range
Yeah, h/w does affects the performance.
Every time I run this in php, I get 5-6 seconds.
What is your h/w?
Apple M1 - 16 GB. This clearly helps.
Yeah, hardware impacts the speed of execution.
Interesting 👍 Could you add Lua please? Very curious about this one but won’t be able to reproduce the setup unfortunately.
good info
Thanks!
In C# you can do this much faster
I was running the folliwing code (using Benchmark)
I runit on Intel i7, 2 core and the results in C# or in milliseconds, not seconds :)
** Standard, your code
** Standard, Assign code (slower)
** Foreach and Range (Slower)
** Foreach (much slower)
I'm surprised C++ is so slow. Almost as if it wasn't compiled with optimizations enabled
-O3
.Yeah, I tried the release mode in VS for C++ and it performs way better (~0.1s).
This is like the most stupid way of doing things i've seen in a while ...
I'll illustrate some differences in PHP.
First of all i will time only the loop inside the script not the whole execution because it has nothing to do with the speed of the code itself.
1)
First way of doing things (the incredible stupid way of doing things illustrated by the OP) using range inside a for loop... for real are we timing creation of arrays or counting ?
2)
Now let's mix it up a little and do something crazy like for example taking out of the loop the part where u generate an array of 1 billion elements...
3)
Now that we got the totally stupid ways of doing things out of the way let's look into more ways to skin this cat ... how about using a while loop since for is not really useful in this case cause we have no need of knowing the current index of the loop.
4)
How about using the language as it is intended, did u know PHP has JIT ? Let's write the code in a form that takes advantage of that.
In conclusion:
Stop misleading people with this BS about one language being better than the other when you obviously use them wrong and have no clue what you are doing.
Someone just starting looking on what language to learn may read your post and take it for real and make a real life decision based on that because he doesn't know any better.
Do you think, in a coding interview, it would be fair to assume solutions in these languages would run at the equal time?
Yes, in coding interviews the choice of programming language doesn't matter.