This is the follow up to my "Write it in C post" and is intended to report the timings for the Java implementation that I said I would write for Charles O Nutter and the Ruby version by Simon Kroeger. First let us deal with the Ruby version.
The program differs from the Perl and C versions in that the various values it requires are not precomputed. Simon's program is completely self contained.
[Latin]$ time ruby latin.rb 5 > r5
real 0m35.793s
user 0m32.081s
sys 0m0.843s
This quite clearly pisses all over the Perl version, and yes the results were correct. Both faster than the Perl version and considerably less code, a testament to the power and expressiveness of Ruby.
Now the Java version. I will be honest here, I might be paid to program in Java but it hasn't been my language of choice since around 1992. I find it gets in my way and today it found yet another way to do it.
A straight translation like the C version worked fine for a 4 x 4 grid but when I got to the 5 x 5 grid I got the following error 'code too large'. Yes Java has hard coded limits as to the allowed size of various data structures within class files and the Compared array of 120 x 120 boolean values could not be initialised with the following code:
private static boolean[][] Compared = {
{false, false, ...
...
{true, true, ...
};
I had to have a whole load of 'Compared[0][44] = true;' and the like to get the data in. This got the 5 x 5 grid to run but the 6 x 6 grid blew up even that. Java has a 64Kb limit for various structures in the class file (see http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html). The last time that I had to work round such mind numbingly arbitrary limits was when I was programming Quick Basic. Now the timings.
[Latin]$ time ./j_version.sh 5 > j5
real 0m29.553s
user 0m13.813s
sys 0m10.745s
Sorry Java fans but "as fast as C" or "faster than C" it is not. It's only a bit faster than Ruby despite having much more resources being dedicated to speeding it up.
The really odd thing here is that Java should actually be much faster than this. I did manage to get the 4 x 4 grid to be written with the same initialisation method as the C version and the timings (admittedly on a much smaller problem) were much closer to the C version for the same 4 x 4 grid. The solution just didn't scale because of the 64Kb limit in the class files, which is probably not going to be change any time in the near future.
In the interest of fairness I also looked at the timings of just the execution of the C and Java version so that the performance of the compilers were not impacting the times. So here is the C and Java versions without the precomuting phase and without the compiling.
[Latin]$ time ./latin > /dev/null 2>&1
real 0m1.961s
user 0m1.680s
sys 0m0.051s
[Latin]$ time java Latin > /dev/null 2>&1
real 0m15.483s
user 0m9.641s
sys 0m4.280s
There you have it, C is still faster by an order of magnitude. Performance is yours for the asking, but it comes at a price - you have to write it in C. Ease of development also comes at a price, you don't get the same performance as C. Of course if you have a fear of C this does show that you can go some of the way by converting to Java, if that is fast enough for you then well and good but know this, C is faster.