1) The document discusses several Java performance myths and uses microbenchmarks to analyze them. It finds that final variables and String concatenation are not necessarily faster than alternatives.
2) It recommends using the Caliper framework for robust microbenchmarking and provides several useful JVM flags for profiling and optimization.
3) The document outlines various Java optimization strategies used by the JVM compiler including inlining, intrinsics, escape analysis, and loop unrolling. It emphasizes the importance of clear and readable code over premature optimization.
2. Micro-Benchmarking
✦ Fictional Test of a Small Code-Snippet
✦ Compare Functionally Equivalent
Implementations
✦ Seek for Arguments in the “Java is Slow”
Battle
10. public class Sample {
static String s = "a" + "b";
}
$javap -c Sample
Compiled from "Sample.java"
public class Sample extends java.lang.Object{
static java.lang.String s;
static {};
Code:
0:! dc! #10; //String ab
l
2:! utstatic! #12; //Field s:Ljava/lang/String;
p
5:! eturn
r
...
21. The Art of Adding Numbers
int sum() {
int result = 0;
for (int i = 0; i < a; i++) {
for (int j = 0; j < b; j++) {
result = result + j;
}
}
return result;
}
22. The Art of Adding Numbers
✦ Plain Imperative: Nested Loops
✦ Extracted Methods (addAll, add)
✦ Parameter Objects (AddParam, AddResult)
✦ Strategies (addAll(strategy)
✦ Fields (this.result = this.a + this.b)
✦ Control Flow By Exception (throw result)
41. Some Advice
✦ [..] Write Readable and Clear Code. [..]
(David Keenan)
✦ [..] slavishly follow a principle of simple, clear
coding that avoids clever optimizations [..]
(Caliper FAQ)
✦ Performance advice has a short shelf-life
(B. Goetz)
42. Credits: I’m thankful to Arno Haase, who allowed to
highjack the title of his great session at
JAX 2012 which inspired me for this talk.