How Fast is Your Java Code

23
How Fast is Your Java Code? Performance micro-benchmarking Dmitry Buzdin

Transcript of How Fast is Your Java Code

Page 1: How Fast is Your Java Code

How Fast is Your Java Code?

Performance micro-benchmarking

Dmitry Buzdin

Page 3: How Fast is Your Java Code

Motivation

Page 4: How Fast is Your Java Code

Programming for Performance

Page 5: How Fast is Your Java Code

SituationSome developers optimize code a lot

Good habits like:* Putting final keyword* Optimizing assignments* Creating less objects* Avoiding reflection

Page 6: How Fast is Your Java Code

Situation

Arguing who’s code is faster

Page 7: How Fast is Your Java Code

So how do you measure that?

Page 8: How Fast is Your Java Code

Macro-benchmarking

vs Micro-

benchmarking

Page 9: How Fast is Your Java Code

Challenge

Page 10: How Fast is Your Java Code

Challenge: How Fast is That?

System.out.println("Hello, World!");

Page 11: How Fast is Your Java Code

DEMO

Page 12: How Fast is Your Java Code

1 millisecond = 1 000 microseconds =

1 000 000 nanoseconds

Page 13: How Fast is Your Java Code

Test Noise

Classloader activitiesReflection inflationGarbage collectionJIT compilation

Page 14: How Fast is Your Java Code

JIT Compiler

Behavior-drivenCan compile and “de-compile”Client and Server JITs are different

Page 15: How Fast is Your Java Code

ManagementFactory.getXYZBean()

Getting JVM Stats

Page 16: How Fast is Your Java Code

ClassLoadingMXBean

GarbageCollectorMXBean

RuntimeMXBean

MemoryMXBean

ThreadMXBean

...

Page 17: How Fast is Your Java Code

Measurement Pitfalls

Measuring in millisecondsNot doing warm-upsNot doing enough warm-upsWrong JVM settingsToo few iterations

Page 18: How Fast is Your Java Code

Conclusions

Page 19: How Fast is Your Java Code

Conclusions

Do not do micro optimizationsWrite first - then measure critical placesProgram readability is more importantDo perform macro optimizations instead

Page 20: How Fast is Your Java Code

“We should forget about small efficiencies, say about 97% of the

time: premature optimization is the root of all evil”

Donald Knuth

Page 22: How Fast is Your Java Code

Code is on GitHub

https://github.com/buzdin/java-microbenchmarking/

Page 23: How Fast is Your Java Code

The End