Skip to content

Commit 2f42b46

Browse files
author
Nicolai Parlog
committed
[06] Create benchmark for String::format
1 parent e6e9e91 commit 2f42b46

File tree

1 file changed

+34
-0
lines changed
  • src/main/java/org/codefx/demo/effective_java/_06_unnecessary_objects

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.codefx.demo.effective_java._06_unnecessary_objects;
2+
3+
import org.openjdk.jmh.annotations.Benchmark;
4+
import org.openjdk.jmh.annotations.BenchmarkMode;
5+
import org.openjdk.jmh.annotations.Fork;
6+
import org.openjdk.jmh.annotations.Measurement;
7+
import org.openjdk.jmh.annotations.Mode;
8+
import org.openjdk.jmh.annotations.OutputTimeUnit;
9+
import org.openjdk.jmh.annotations.Param;
10+
import org.openjdk.jmh.annotations.Scope;
11+
import org.openjdk.jmh.annotations.State;
12+
import org.openjdk.jmh.annotations.Warmup;
13+
14+
import java.util.concurrent.TimeUnit;
15+
16+
public class StringFormat extends ObjectCreationBenchmarks {
17+
18+
@Param({ "Motoko", "Batou" })
19+
private String name = "_";
20+
21+
@Benchmark
22+
public String format() {
23+
// NOTE unnecessary object:
24+
// The JVM has to create an array for `name` and `9`
25+
// and box the latter into an `Integer`
26+
return String.format("%s(Section %d)", name, 9);
27+
}
28+
29+
@Benchmark
30+
public String concatenate() {
31+
return name + "(Section " + 9 + ")";
32+
}
33+
34+
}

0 commit comments

Comments
 (0)