Saturday, November 14, 2009

Which language is fastest? ;-)

Update: 15/01/2010 - It seems that I made big bug in test code for C# which caused decreasing of C# results about factor 10! I assumed that DateTime.New.Ticks returns nanoseconds, but it returns ticks, and in one nanosecond we have 10 ticks... Sadly nobody looked into sources of my tests :-(
So results for C# should be divided by 10, and in this case C# is faster than Java about 3 times [but not Java 1.5 with -server switch used in this case Java is a little faster than C#].
New results for Intel Core 2 Duo T6600 2.20 GHz under 64 bit Windows 7:
C# z .NET 4.0 - 1.17 ms
Java 1.5 with switch -server - 1.096 ms
Java 1.6 with switch -server - 2.8895 ms
Python 2.6 - 10.88 ms

Recently I made tests of this which programming language gives quicker code ;-)
Of course it depends from this what this code should to do.
So I choose thing which I'm writing almost always in new [new for me] language :-)

I wrote it even during my first session, over green screen [on RS232 connection] when I was first time connected to HP-UX on my University server ;-) [or maybe not??? I'm not sure now ;-)]

This thing is simple numerical integration of function f(x)=e-x2 in range (-10,10> [when calculated form minus infinity to plus infinity result is equals to square root from Pi :-), I was really impressed when my professor showed it to us on our lecture from mathematical analysis]. Those calculations are performed in 10000 of steps, and repeated 1000 times for stable result [so we measure time of 1000 iterations from 0 to 9999] and divide result by 1000 to get information about time needed to calculate one iteration.

If you are interested in source codes of programs I wrote for those tests, please use one of those links [those are links to my blog in Polish, but you needn't be afraid ;-) you are interested only in source codes] sources for Java, Python, C, and C# [here you may find ZIP file with some of those programs], sources for JavaScript [here is a simple JavaScript tester, which will display info how long took one full iteration for calculate this numerical integration], and finally sources for Go [here you need to be warned, that this Go program should have changed one line, where math.Exp() is used, it should use math.Pow()].

And results are interesting ;-)

C++ 32bit - 0.985 ms
C++ 64bit - 1.387 ms
JavaScript from Mozilla Firefox 3.5.4 - 1.380 ms
JavaScript from Google Chrome - 2.514 ms
Java 64bit - 3.8 ms
Java 32bit - 4.7 ms
Google Go 32bit - 8.521 (5.68) ms
Python 2.5.2 32bit - 10.3 ms
Python 2.5.4 64bit - 10.16 ms
C# 64bit - 14 ms

All tests except test for Go was performed on my 64 bit Windows 7 laptop with Core 2 Duo processor, Go was tested on virtual Ubuntu 9.10 in VMware Player 3.0 hosted on Windows 7, in case of Go first result [8.521 ms] is a result shown by test program, second result [5.68 ms] was calculated by comparing results of JavaScript from Firefox on Windows 7 and in this virtual Ubuntu 9.10.
C++ code was compiled with Visual C++ 2008 Express Edition [for 64 bits I used compiler from Windows 7 SDK, here instruction how to make Visual C++ 2008 Express Edition to use it :-)]

The biggest surprise for me was really poor result of C#, and superb result of JavaScript in FireFox :-) I still don't know why JS was so fast :-) [interesting thing also is this that FireFox JavaScript was here faster than Google Chrome JavaScript ;-)}
Result of Go wasn't so surprising, now after the premiere of this language we can here only praises, but its still experimental thing, and I used 32 bit version of compiler which isn't the best ;-) [I must also admit that after my first experience with Go I don't like it and I hope this one Google project will die].

I think that good advice after this test is ;-) If you need to write some very quick code use C++, if it must be fast but needn't to be extremal fast - use Java, and newer use C# ;-)

Similar postsbeta
Calculation of SSD life time in EEE PC
Sources of JPC - x86 emulator in Java
How to detect clickbaits?
Programmers love to over complicate...
Gouraud Shading in JavaScript :-)

1 comment:

  1. Interesting post! I've always wondered about that. Keep it up.