How Fast is Your Java Code

Post on 10-May-2015

586 views 1 download

Tags:

Transcript of How Fast is Your Java Code

How Fast is Your Java Code?

Performance micro-benchmarking

Dmitry Buzdin

About Speaker

Dmitry Buzdin

buzdin@gmail.com

@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

Code is on GitHub

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

The End