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.

Comments (34)

Add a comment
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 all replies
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

Show all replies
Viktor Georgiev's photo

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

print sum(xrange(100000000))