Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to count number of bytecodes executed in java

Tags:

java

jvm

bytecode

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.)

like image 214
andyInCambridge Avatar asked Dec 10 '12 23:12

andyInCambridge


1 Answers

You can get the count by using a debug build of the Hotspot JVM (which can be found here) and running it with the -XX:+CountBytecodes flag.

like image 173
int3 Avatar answered Nov 02 '22 19:11

int3