Daily Archives: 2012-06-07


Java vs C# vs Javascript

*** UPDATE: This article had a HUGE mistake in the Javascript part, read below ***

I’m a Java guy but I’m always opened to other programming languages. I heard great things about Mono and the .Net platform, among those things recently I saw a Xamarin article about Android being ported over Mono in a project called XobotOS (http://blog.xamarin.com/2012/05/01/android-in-c-sharp/) and they are getting a much better performance than with Dalvik.

They did it using Sharpen: an automated tool that can convert from Java source code to C#. I was hesitated and started to port my “Carballo” chess engine to C#. Sharpen runs as an eclipse plugin (but outside eclipse). The best tutorial that I found to install sharpen is here: http://www.pauldb.me/post/14916717048/a-guide-to-sharpen-a-great-tool-for-converting-java

The new Carballo Chess Engine with a C# version is now hosted at GitGub (https://github.com/albertoruibal/carballo). You can launch the conversion tool running “ant” on the “csharp/” folder. This C# version hasn’t a graphic interface yet, but is great to make some comparisons…

I use a lot the BT2630 chess test suite, with positions to find the best move, lets see how much time needs my chess engine in Java to find the solution for the first position:

r q . . r . k . 8
. . . . . p p . 7
p . . . . . . . 6
. . . . b N P . 5
. p . . P . . P 4
. . . . . Q . . 3
P P . . . . K . 2
. . . . . R . R 1
a b c d e f g h white move
DEBUG SearchEngine - depth 1 score cp 170 nodes 176 time 42 nps 4093 pv f3b3
DEBUG SearchEngine - depth 2 score cp 136 nodes 1044 time 123 nps 8419 pv f3b3 b8b7
DEBUG SearchEngine - depth 3 score cp 131 nodes 4335 time 358 nps 12075 pv f1f2 b8b6 f3b3
DEBUG SearchEngine - depth 4 score cp 111 nodes 14603 time 854 nps 17079 pv f1f2 b8b6 f3b3 e8e6
DEBUG SearchEngine - depth 5 score cp 115 nodes 27593 time 1311 nps 21031 pv f1f2 b8b7 h1d1 a8d8 f2d2
DEBUG SearchEngine - depth 6 score cp 119 nodes 50536 time 1450 nps 34828 pv f1f2 b8b7 h1d1 a8d8 f2d2 d8d2
DEBUG SearchEngine - depth 7 score cp 117 nodes 110415 time 1693 nps 65180 pv f1f2 b8b7 h1d1 a8c8 f2d2 b7c6 b2b3
DEBUG SearchEngine - depth 8 score cp 110 nodes 314754 time 2515 nps 125100 pv f1f2 b8b7 h1d1 a8d8 f2d2 d8d2 d1d2 b7c6 b2b3
DEBUG SearchEngine - depth 9 score cp 112 nodes 564320 time 3580 nps 157587 pv f1f2 b8b7 h1d1 a8d8 f2d2 d8d2 d1d2 a6a5 b2b3 b7b6
DEBUG SearchEngine - depth 10 score cp 129 nodes 1219488 time 6174 nps 197487 pv f1f2 b8c7 f3b3 c7b7 b3d5 b7d5 e4d5 a8d8 h1d1 e8e6 a2a4
DEBUG SearchEngine - depth 11 score cp 241 nodes 2430413 time 11373 nps 213681 pv f5g7 e8f8 g7f5 a8a7 f5h6 g8g7 f1f2 b8b5 h6f5 g7g8 f3b3 a7d7 h1c1 d7d1

This is 11.373 seconds at 213.681 nodes per second, my java version is “Java(TM) SE Runtime Environment (build 1.6.0_26-b03)”.

And with Mono? let’s see the result on the same machine:

DEBUG SearchEngine - depth 1 score mate 0 nodes 176 time 25 nps 6769 pv f3b3
DEBUG SearchEngine - depth 2 score mate 0 nodes 1044 time 36 nps 28216 pv f3b3 b8b7
DEBUG SearchEngine - depth 3 score mate 0 nodes 4335 time 58 nps 73474 pv f1f2 b8b6 f3b3
DEBUG SearchEngine - depth 4 score mate 0 nodes 14603 time 136 nps 106591 pv f1f2 b8b6 f3b3 e8e6
DEBUG SearchEngine - depth 5 score mate 0 nodes 27593 time 247 nps 111262 pv f1f2 b8b7 h1d1 a8d8 f2d2
DEBUG SearchEngine - depth 6 score mate 0 nodes 50536 time 450 nps 112053 pv f1f2 b8b7 h1d1 a8d8 f2d2 d8d2
DEBUG SearchEngine - depth 7 score mate 0 nodes 110415 time 813 nps 135644 pv f1f2 b8b7 h1d1 a8c8 f2d2 b7c6 b2b3
DEBUG SearchEngine - depth 8 score mate 0 nodes 314754 time 2058 nps 152867 pv f1f2 b8b7 h1d1 a8d8 f2d2 d8d2 d1d2 b7c6 b2b3
DEBUG SearchEngine - depth 9 score mate 0 nodes 564320 time 3693 nps 152766 pv f1f2 b8b7 h1d1 a8d8 f2d2 d8d2 d1d2 a6a5 b2b3 b7b6
DEBUG SearchEngine - depth 10 score mate 0 nodes 1219488 time 7673 nps 158911 pv f1f2 b8c7 f3b3 c7b7 b3d5 b7d5 e4d5 a8d8 h1d1 e8e6 a2a4
DEBUG SearchEngine - depth 11 score mate 0 nodes 2430413 time 15490 nps 156891 pv f5g7 e8f8 g7f5 a8a7 f5h6 g8g7 f1f2 b8b5 h6f5 g7g8 f3b3 a7d7 h1c1 d7d1

That is 15.490 seconds at 156.891 nodes per second with the “Mono JIT compiler version 2.10.8.1 (Debian 2.10.8.1-3)”.

I didn’t found the Sharp and Mono magic, at least in my case Mono is about a 30% slower than Java. This may be as a result of the automated code conversion…

I tested the GWT part in the GWT Developer Mode. In this mode, the code is run in the server as Java (and not in JS). In recent tests I found that the Javascript version is much more slow, probably because of the GWT Javascript Long emulation (Js does not has 64bits integers).

Well and this chess engine also has a GWT version. GWT is another magical Google product that converts your java code to Javascript. Let’s see how fast runs this test on the browser:

[INFO] [org.vectomatic.libgwtsvgchess] - Main.info(depth 1 score cp 170 nodes 176 time 56 nps 3087 pv f3b3 )
[INFO] [org.vectomatic.libgwtsvgchess] - Main.info(depth 2 score cp 136 nodes 1044 time 186 nps 5582 pv f3b3 b8b7 )
[INFO] [org.vectomatic.libgwtsvgchess] - Main.info(depth 3 score cp 131 nodes 4335 time 483 nps 8956 pv f1f2 b8b6 f3b3 )
[INFO] [org.vectomatic.libgwtsvgchess] - Main.info(depth 4 score cp 111 nodes 14603 time 997 nps 14632 pv f1f2 b8b6 f3b3 e8e6 )
[INFO] [org.vectomatic.libgwtsvgchess] - Main.info(depth 5 score cp 115 nodes 27593 time 1123 nps 24548 pv f1f2 b8b7 h1d1 a8d8 f2d2 )
[INFO] [org.vectomatic.libgwtsvgchess] - Main.info(depth 6 score cp 119 nodes 50536 time 1251 nps 40364 pv f1f2 b8b7 h1d1 a8d8 f2d2 d8d2 )
[INFO] [org.vectomatic.libgwtsvgchess] - Main.info(depth 7 score cp 117 nodes 110415 time 1571 nps 70238 pv f1f2 b8b7 h1d1 a8c8 f2d2 b7c6 b2b3 )
[INFO] [org.vectomatic.libgwtsvgchess] - Main.info(depth 8 score cp 110 nodes 314754 time 2648 nps 118819 pv f1f2 b8b7 h1d1 a8d8 f2d2 d8d2 d1d2 b7c6 b2b3 )
[INFO] [org.vectomatic.libgwtsvgchess] - Main.info(depth 9 score cp 112 nodes 564320 time 4063 nps 138858 pv f1f2 b8b7 h1d1 a8d8 f2d2 d8d2 d1d2 a6a5 b2b3 b7b6 )
[INFO] [org.vectomatic.libgwtsvgchess] - Main.info(depth 10 score cp 129 nodes 1219488 time 7474 nps 163142 pv f1f2 b8c7 f3b3 c7b7 b3d5 b7d5 e4d5 a8d8 h1d1 e8e6 a2a4 )
[INFO] [org.vectomatic.libgwtsvgchess] - Main.info(depth 11 score cp 241 nodes 2430413 time 14216 nps 170951 pv f5g7 e8f8 g7f5 a8a7 f5h6 g8g7 f1f2 b8b5 h6f5 g7g8 f3b3 a7d7 h1c1 d7d1 )

I can’t believe my eyes : 14.216 seconds at 170.951 nodes per second with Google Chome Version 19.0.1084.52… and this is also an automated conversion process…

Yes, in my tests Javascript is faster than Sharpened Mono…