What does OpenJDK JMH “score error” exactly mean?

This is the margin of error for the score. In most cases, that is a half of confidence interval. Think about it as if there is a “±” sign between “Score” and “Score error”. In fact, the human-readable log will show that: Result: 1.986 ±(99.9%) 0.009 ops/ns [Average] Statistics: (min, avg, max) = (1.984, 1.986, … Read more

Why is the StringBuilder chaining pattern sb.append(x).append(y) faster than regular sb.append(x); sb.append(y)?

String concatenation a + b + c is a very frequent pattern in Java programs, so HotSpot JVM has a special optimization for it: -XX:+OptimizeStringConcat which is ON by default. HotSpot JVM recognizes new StringBuilder().append()…append().toString() pattern in the bytecode and translates it to the optimized machine code without calling actual Java methods and without allocating … Read more

Getting an accurate execution time in C++ (micro seconds)

If you are using c++11 or later you could use std::chrono::high_resolution_clock. A simple use case : auto start = std::chrono::high_resolution_clock::now(); … auto elapsed = std::chrono::high_resolution_clock::now() – start; long long microseconds = std::chrono::duration_cast<std::chrono::microseconds>( elapsed).count(); This solution has the advantage of being portable. Beware that micro-benchmarking is hard. It’s very easy to measure the wrong thing (like … Read more

tech