DEV Community

Cover image for Count to 1 billion
Ishaan Sheikh
Ishaan Sheikh

Posted on • Updated on • Originally published at frikishaan.com

Count to 1 billion

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
Enter fullscreen mode Exit fullscreen mode

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)

Collapse
 
giulio profile image
Giulio "Joshi" • Edited

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.

<?php
$i=0;
while($i < 1000000000){
        $i++;
}
Enter fullscreen mode Exit fullscreen mode

This little guy got 26 seconds to run:

$: time php count.php 

real    0m26,712s
user    0m26,504s
sys     0m0,046s
Enter fullscreen mode Exit fullscreen mode

Node 18. performed slightly better on my HW:

let i = 0;
while (i < 1000000000){
    i++;
}
Enter fullscreen mode Exit fullscreen mode
time node index.js 

real    0m0,888s
user    0m0,873s
sys     0m0,010s
Enter fullscreen mode Exit fullscreen mode

While Rust took almost no time:

fn main() {
    let mut n  = 0;
    while n <= 1000000000 {
        n+=1;
    }
}
Enter fullscreen mode Exit fullscreen mode
❯ cargo build --release
...
❯ time target/release/count

real    0m0,004s
user    0m0,003s
sys     0m0,001s

Enter fullscreen mode Exit fullscreen mode

With debug symbols the situation changes perceptibly:

❯ cargo build 
...
❯ time target/debug/count 

real    0m3,315s
user    0m3,291s
sys     0m0,008s
Enter fullscreen mode Exit fullscreen mode

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++.

Collapse
 
frikishaan profile image
Ishaan Sheikh

I had built the code using rustc. I have tried using the cargo build --release and it took no time. Thanks.

Collapse
 
thedenisnikulin profile image
Denis

please edit your post then, it can lead to confusion about Rust performance

Collapse
 
phlash profile image
Phil Ashby

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 😄

Collapse
 
cicirello profile image
Vincent A. Cicirello

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."

Collapse
 
frikishaan profile image
Ishaan Sheikh

Yes that may be the reason for the performance.

Collapse
 
superfola profile image
Alexandre Plateau

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?

Collapse
 
frikishaan profile image
Ishaan Sheikh

No, I compiled all in Debug mode. I just tried C++ in release mode and it executes faster (~0.1s).

Collapse
 
nikfp profile image
Nik F P

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.

Collapse
 
jmfayard profile image
Jean-Michel Fayard 🇫🇷🇩🇪🇬🇧🇪🇸🇨🇴 • Edited

Python is the worst performer. I don't know the exact reason for this behavior

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++

Collapse
 
codingjlu profile image
codingjlu

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!)

Collapse
 
devshogun profile image
Michael Essiet

Hmmmm, I got quite different results on the M1 Pro. All tests fell withing the 1-4 secs range

Collapse
 
frikishaan profile image
Ishaan Sheikh

Yeah, h/w does affects the performance.

Collapse
 
francisc profile image
Fran Sancisco

Every time I run this in php, I get 5-6 seconds.

Collapse
 
frikishaan profile image
Ishaan Sheikh

What is your h/w?

Collapse
 
francisc profile image
Fran Sancisco

Apple M1 - 16 GB. This clearly helps.

Thread Thread
 
frikishaan profile image
Ishaan Sheikh

Yeah, hardware impacts the speed of execution.

Collapse
 
fyodorio profile image
Fyodor

Interesting 👍 Could you add Lua please? Very curious about this one but won’t be able to reproduce the setup unfortunately.

Collapse
 
jonatelo09 profile image
Jonatan Arevalo

good info

Collapse
 
frikishaan profile image
Ishaan Sheikh

Thanks!

Collapse
 
frederik_vl profile image
Frederik Van Lierde

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 :)

Image description

** Standard, your code

public void CountTo1BPlusPlus()
        {
            int count = 0;
            for (int i = 0; i < 1000000000; i++)
                count++;
        }
Enter fullscreen mode Exit fullscreen mode

** Standard, Assign code (slower)

public void CountTo1B()
        {
            int count = 0;
            for (int i = 0; i < 1000000000; i++)
                count=i;
        }
Enter fullscreen mode Exit fullscreen mode

** Foreach and Range (Slower)

public void CountTo1BRange()
        {
            int count = 0;
            foreach (int index in Enumerable.Range(0, 1000000000))
                count++;
        }
Enter fullscreen mode Exit fullscreen mode

** Foreach (much slower)

 public void CountTo1BForEach()
        {
            int count = 0;
            Enumerable.Range(1, 1000000000).ToList().ForEach(i => count++);
        }
Enter fullscreen mode Exit fullscreen mode
Collapse
 
eljayadobe profile image
Eljay-Adobe

I'm surprised C++ is so slow. Almost as if it wasn't compiled with optimizations enabled -O3.

Collapse
 
frikishaan profile image
Ishaan Sheikh • Edited

Yeah, I tried the release mode in VS for C++ and it performs way better (~0.1s).

Collapse
 
raizeno profile image
raizeno

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 ?

Image description

Image description

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...

Image description

Image description

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.

Image description

Image description

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.

Image description

Image description

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.

Collapse
 
akonihakeem profile image
Akoni Mayowa Hakeem

Do you think, in a coding interview, it would be fair to assume solutions in these languages would run at the equal time?

Collapse
 
frikishaan profile image
Ishaan Sheikh

Yes, in coding interviews the choice of programming language doesn't matter.