File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed
src/main/java/org/codefx/demo/effective_java/_06_unnecessary_objects Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments