I’m working on a Sudoku solver at school and we’re having a little performance contest. Right now, my algorithm is pretty fast on the first run (about 2.5ms), but even faster when I solve the same puzzle 10 000 times (about 0.5ms for each run). Those timing, of course, depend of the puzzle being solved. I know the JVM do some optimization when a method is called multiple time, and this is what I suspect is happening.
I don’t think I can further optimize the algorithm itself (though I’ll keep looking), so I was wondering if I could replicate some of the optimizations done by the JVM.
Note : compiling to native code is not an option
Thanks!
Edit : All those VM options are nice, but are not really “legal” in an algorithm contest, since everyone could use those options and get the performance boost. I’m looking for code optimization.
tl;dr: no, most optimizations done by the JVM can’t be expressed in Java byte code.
By its very nature Java byte code is defines your code on a very high level.
That is by design and is one of the reason why the JVM can do all that optimizations: byte code describes the operations on a relatively high level and leaves the actual execution details to the JVM.
Due to that fact most optimizations can’t be expressed in byte code itself.
For example the JVM spec specifies that every array access that exceeds the bounds of the array must throw a
ArrayIndexOutOfBoundsException(see VM Spec 2.5.14).However, a smart JVM can optimize away many of those checks if it knows that they already happend (for example when iterating over an array, then the JVM can actually execute the bounds-check before the iteration and skip it during each iteration).
This optimization simply can’t be expressed with byte code, because the bounds-check is implicit and can’t be avoided in the byte-codes representing array access.