问题
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.)
回答1:
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.
回答2:
Looking over their source code, they use the asm bytecode manipulator http://asm.ow2.org/ to do the counting.
回答3:
You can generate your bytecode like this:
javac Employee.java
javap -c Employee > Employee.bc
Source: http://www.ibm.com/developerworks/ibm/library/it-haggar_bytecode/
来源:https://stackoverflow.com/questions/13811082/how-to-count-number-of-bytecodes-executed-in-java