How Fast is Your Java Code
-
Upload
neueda -
Category
Technology
-
view
586 -
download
1
Transcript of How Fast is Your Java Code
How Fast is Your Java Code?
Performance micro-benchmarking
Dmitry Buzdin
About Speaker
Dmitry Buzdin
@buzdin
Motivation
Programming for Performance
SituationSome developers optimize code a lot
Good habits like:* Putting final keyword* Optimizing assignments* Creating less objects* Avoiding reflection
Situation
Arguing who’s code is faster
So how do you measure that?
Macro-benchmarking
vs Micro-
benchmarking
Challenge
Challenge: How Fast is That?
System.out.println("Hello, World!");
DEMO
1 millisecond = 1 000 microseconds =
1 000 000 nanoseconds
Test Noise
Classloader activitiesReflection inflationGarbage collectionJIT compilation
JIT Compiler
Behavior-drivenCan compile and “de-compile”Client and Server JITs are different
ManagementFactory.getXYZBean()
Getting JVM Stats
ClassLoadingMXBean
GarbageCollectorMXBean
RuntimeMXBean
MemoryMXBean
ThreadMXBean
...
Measurement Pitfalls
Measuring in millisecondsNot doing warm-upsNot doing enough warm-upsWrong JVM settingsToo few iterations
Conclusions
Conclusions
Do not do micro optimizationsWrite first - then measure critical placesProgram readability is more importantDo perform macro optimizations instead
“We should forget about small efficiencies, say about 97% of the
time: premature optimization is the root of all evil”
Donald Knuth
Some Resourceshttp://www.ibm.com/developerworks/java/library/j-benchmark2/
index.html
http://stackoverflow.com/questions/504103/how-do-i-write-a-correct-micro-benchmark-in-java
http://docs.oracle.com/javase/specs/jvms/se7/html/index.html
Code is on GitHub
https://github.com/buzdin/java-microbenchmarking/
The End