Saturday, February 12, 2011

Fibonnaci() - JavaScript (Node.Js v.0.2.6) versus C# 4.0 on Windows XP 32b

In my previous post I compared the speed of execution of Python, Ruby and JavaScript V8 computing the Fibonnaci() function written in iterative mode.
The speed of JavaScript V8, is so incredible that I tried comparing it against C#.
In this sample, C# is twice faster but still, this explain why JavaScript and Node.js
are so popular.

JavaScript V8 tested on Node.js

function Fibonnaci(n){

    var previous = -1;
    var result   =  1;

    for(var i=0; i < n+1; i++){

        var sum  = result + previous;
        previous = result;
        result   = sum;
    }
    return result;
}

console.log("Node.js "+new Date());

for(var t=0; t < 1000; t++){

    for(var i=0; i < 1000; i++){

        var v = Fibonnaci(i);
    }
}
console.log(new Date());



C# .NET 4.0 x86

static long Fibonnaci(long n){

    long previous = -1;
    long result   =  1;
    for(var i=0; i < n+1; i++){
        long sum  = result + previous;
        previous = result;
        result   = sum;
    }
    return result;
}
static void Main(string[] args) {

    Console.WriteLine("C# 4.0 {0}", DateTime.Now);

    for(var t=0; t < 1000; t++){

        for(var i=0; i < 1000; i++){

            var v = Fibonnaci(i);
        }
    }
    Console.WriteLine("{0}", DateTime.Now);
    Console.ReadLine();
}



The results


JavaScript
Sat Feb 12 2011 06:04:11
Sat Feb 12 2011 06:04:17
6 seconds

C#
2/12/2011 1:10:53 AM
2/12/2011 1:10:56 AM
3 seconds

No comments:

Post a Comment