How to count number of bytecodes executed in java
bytecode, java, jvm
Solution
Looking over their source code, they use the asm bytecode manipulator http://asm.ow2.org/ to do the counting.
Problem
I'm going to enter MIT's battlecode competition. The entrants write programs that control robots that fight each other. The catch is that your robots are limited to executing a certain amount of bytecode in a turn (last year it was 10000 per turn). Now, a simple loop like ``` (int i=0; i<100; i++){ // do nothing } ``` uses, according to their software, approximately 400 bytecode (presumably something like (2 bytecode for incrementing i plus 2 bytecode for checking if i<100) * 100 = 400 bytecode) so we have to write very tight code. Hence, as I try out some different navigation algorithms its important that I be able to figure out how much bytecode my code is using -- how can I do this? (It IS possible -- they do it, I'm just not sure how! Also, they must stop the JIT from coming into play somehow. I know that each robot is run in a separate Thread, so I'm sure the answer involves some sort of Thread trickery I don't know about.)