Comparison: Node.js, PHP, C, Go, Python and Ruby

I had fun doing performance tests after reading an article which had performance tests for Python.

I wanted to only test web back-end tech, but I still added in a test with C in order to compare everythig else with a low-level language.

Here is the code:

Python

sum = 0
for i in range(100000000):
    sum += i
print(sum)

Ruby

#!/usr/bin/ruby

$sum = 0
for $i in 0..100000000
      $sum += $i
end
puts($sum)

Node.js

#!/usr/bin/env

var sum = 0
for (var i = 0; i < 100000000; i++) {
    sum +=i
}
console.log(sum);

PHP

#!/usr/bin/php
<?php
$sum = 0;
for ($i = 1; $i < 100000000; $i++) {$sum += $i;}    echo $sum + '\n'?>

Go

package main

import "fmt"

func main() {
    sum := 0
    for i := 0; i < 100000000; i++ {
        sum += i
    }
    fmt.Println(sum)
}

C

#include <stdio.h>

int main ()
{
  int a;
  int sum = 0;
  for( a = 0; a < 100000000; a++ )
    {
      sum += a;
    }
  printf("sum: %d\n", sum);
  return 0;
}

Results:

Result

And the winner is... GO !

PS : Go here is 9x better than than C, but with good optimisation flags, C is faster.

Learn Something New Everyday,
Connect With The Best Developers!

Sign Up Now!

& 500k+ others use Hashnode actively.

Dong Nguyen's photo

My machine is Core I3, 8GB of RAM, SSD of course :) It took 7 - 8 seconds with Python and 0.02 - 0.12 with Node.js But the most interesting thing is when I change 100 mils to 1 bil, Python consumed the whole memory and then printed "Killed" after about 1 minute, while Node.js returned the result within only 0.853 sec!

Show +3 replies
Louis's photo

I think you choose the best decision i'm coding in go since 6 month. I think this langage is very elegant and performing :)

tom hasc's photo

Using python's range while using for() in other languages isn't fair. In python either use simple while loop or xrange.

Louis C optimizes the loop into a simple formula, so it does not run the loop.

Patrick Metzdorf's photo

Yes, the Python performance is standing out, isn't it. I heard it has that reputation, i.e. that one shouldn't use it if performance is a big requirement.

Glad to see Node doing so well.

I'm awed at Go, though. I feel its got a lot of momentum these days but I hesitate to spend much time on it and will want to wait and see how the ecosystem is developing. Performance isn't everything ;)

Louis's photo

Yes you're right, performance isn't everything, but performance is the better way to innovation :)

Ryan Gonzalez's photo

Out of curiosity, what happens if you run the Python version under PyPy?

Also, if you're using Python 2, note that range retuens a list. So, it first generates this HUGE list of numbers, then calculates the sum. Try switching to xrange, or, better yet:

print(sum(xrange(100000000)))

Denny Trebbin's photo

You can write more elegant Ruby code (and it should be faster too).

#!/usr/bin/ruby

sum = 0
(1..100000000).each { |i| sum += i } 
puts sum
Felix Heck's photo

Python >11 seconds :O

Dong Nguyen's photo

Yes, I still through that Python must be faster than PHP and Ruby :(

Viktor Georgiev's photo

for this calculation it is if you actually write the correct code which is:

print sum(xrange(100000000))

PrasannaKumar Muralidharan's photo

Unfortunately your test output for C is wrong. Variable sum should be declared as 'long'. In Lenovo G50-80 - Python: 11s, ruby: 10s, nodejs: 0.192s, C: 0.01s, Java: 0.112s, Go: 0.334s.

PS: ruby gives 5000000050000000 while all others give 4999999950000000 as the sum.

Smith Aitufe's photo

Software developer

print(sum(xrange(100000000))) gives 4999999950000000

while

print(sum(xrange(100000001))) gives 5000000050000000

Then it is safe to conclude that the first one is actually from 1 to 99999999 and 100000001 is 1 to 100000000

Coder Yeti's photo

python does it faster (~5 seconds) if you do:

print(sum(range(100000000))) 

(or xrange on P2)

but still last position, alas :(

Bruno Monteiro's photo

Using C#:

static void Main() 
{
    long sum = 0;
    for (var i = 0; i < 100000000; i++) sum +=i;
    Console.WriteLine(sum);
}

// Output
4999999950000000
0.27s
Arvind Kumar's photo

How about some pythonic way of doing it?

Your code returns in about 20 seconds on my machine. The following one-liner takes ~9 seconds:

print sum(xrange(100000000))
Smith Aitufe's photo

Software developer

I wish to comment more on my observations with nodejs

var sum = 0
for (var i = 0; i < 100000000; i++) {
    sum +=i //this is slower. Use sum = sum + 1
    //sum += 1 gave me 1.963235s on my machine whereas
    //sum = sum + 1 gave me 0.506995s
}
console.log(sum);        

//I also rewrote it using while loop and the performance increase

var sum = 0;
limit = 100000000;
while(limit > 0)
{
    sum = sum + limit--;
}
console.log(sum)

//I noticed the summation was completed in 0.214555s 

//The ram of my system is 6GB, core i3.
Roberto Lo's photo

Prettier Python:

print (sum(xrange(100000000)))

I am on my cellphone, so I can't tell about performance 😃

Károly Kiripolszky's photo

Although languages don't have performance, implementations do. For example nuitka is considerably faster than CPython. And Python 2.x is faster than Python 3.x.

Gary Heath's photo

On my 2010 Mac:

time perl6 -e 'say [+] ^100000000' #=> 4999999950000000 0.2 secs

time python3 -c 'print(sum(range(100000000)))' #=> 4999999950000000 5.1 secs

Simon Hamp's photo

Interesting, but to be really useful we need a lot more detail about your machine and the setup of each environment e.g. version numbers.

For example, my PHP 7.0.12 on a 2012 MacBook Pro 2.3GHz i7 completes this in avg. ~7.5 seconds.

Hugo Delgadinho's photo

Interesting experimentation, but I suggest this article for a better understanding of the differences between node.js and ruby

j's photo

I am missing so much data. what versions of the programming language were taken? what is the memory footprint?

why does anyone believe a loop of + operations represent a plausible benchmark?

the types in the background are important they allocate size .... in the end ... this is nice but it's meaningless.

even the CPU architecture is key and which language supports your local settings ....

this comparison is the classic superficial approach. And I don't mean it's bad what you did .... I just say it's 1 of n ... let's stay scientific

put it out of the CPU page range, increase the sets compare the memory as well.

try bitshifting binary additions ... and so on. Try it on different CPUs and architectures.

different versions of the programming languages.

David Ravinkat's photo

Good job. Here I got a kindergarten for you ;-)

Jhourlad Estrella's photo

I think the most fair way to do this test is to loop with random strings. C can easily convert sequential integers into optimized algorithms which does not quite reflect real life scenarios.

Alexey Porubay's photo

Hello everyone! I made an article were I compare Python vs PHP vs Java vs Ruby vs C#. Can you check it and say your opinion about it? (This is not a purely technical material, but more generalizing for beginners) cleveroad.com/blog/python-vs-other-programm..

Viktor Georgiev's photo

and python way is this:

print sum(xrange(100000000))

Also much faster. The way you are doing it in python you are creating a list (array) and then looping trough it. That's why it's so slow and you also may run out of memory

Viktor Georgiev's photo

php code is not optimal too :)

$sum = 0;

for ($i = 1; $i < 100000000; ++$i) {

$sum = $sum + $i;

}

echo $sum . PHP_EOL;

This was about 50% faster during my testing

Steven Ventimiglia's photo

Woah... I just found this and it blows my mind. I thought Python, especially being a language that Google's been hiring for, was the bees knees. Now, I'm glad I never touched it seriously.

And, even crazier is that PHP is given such a slap by people, but it pretty much is just as speedy as Ruby. I'm now wondering if Ruby is a better language to write than PHP, or if it's got similar issues programmers complain about with PHP.

Node is all fine and dandy, however, Go seems much more grounded since it seems to offer way more scalability with much less of a tendency to become a "hot mess".

Now, my question would be:

Which is easier for a first time programmer to learn, Ruby or Go?

j's photo

go is easier, it's a minimal langue set.

php always has been faster than ruby since php 5.2 to my knowledge. It's just a different language design.

python is not optimized for those operations and the question is the second run and if it's using pypy jit .....

still the benchmark is 1 of n hence irrelevant.

Go gets slow with the GC this is whole operation is on the stack .... I cannot begin to express how annoyed I am with such shots from the hip benchmarks, which don't take into account the language design.

And give up certain impressions but where is for example the memory footprint .... anyhow I getting am old ....

Steven Ventimiglia's photo

Creative Technologist & Sr. Front-End Developer

Same here. My personal dev-evolution, thus far, has been; Assembly, Turbo Pascal, VBScript, PHP, Node.js, Go.

Sprinkle in some Ruby (on Rails), Python, and Coldfusion.

Things change way too often these days, so Go is my language of choice since it's built by legends for the purpose of fixing the unfixable issues of other programming languages.

It's also fun to learn, fairly simple, and is all set for the long-term lifespan the current industry seems to ignore, but is still super important.