blob: 13767c8034ea7309a82d34792c17027f6bae67fe (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.text;
import org.junit.Test;
/**
* Compares alternative ways of appending strings
*
* @author bratseth
*/
public class StringAppendMicroBenchmarkTest {
private static abstract class Benchmark {
private int repetitions=10000000;
public void execute() {
System.out.println("Executing benchmark '" + getName() + "' ...");
append(100000); // warm-up
long start=System.currentTimeMillis();
append(repetitions);
long duration=System.currentTimeMillis()-start;
System.out.println("Completed " + repetitions + " repetitions in " + duration + " ms\n");
}
private int append(int repetitions) {
String prefix="hello";
int totalSize=0;
for (int i=0; i<repetitions; i++) {
String full=appendStrings(prefix, String.valueOf(i));
totalSize+=full.length();
}
return totalSize;
}
protected abstract String getName();
protected abstract String appendStrings(String a,String b);
}
private static final class PlusOperatorBenchmark extends Benchmark {
@Override
protected String getName() { return "Plus operator"; }
@Override
protected String appendStrings(String a, String b) {
return a+b;
}
}
private static final class StringConcatBenchmark extends Benchmark {
@Override
protected String getName() { return "String concat"; }
@Override
protected String appendStrings(String a, String b) {
return a.concat(b);
}
}
/**
* Make Clover shut up about this in the coverage report.
*/
@Test
public void shutUpClover() {
}
public static void main(String[] args) {
new PlusOperatorBenchmark().execute(); // Typical number on my box with Java 7: 1000 ms
new StringConcatBenchmark().execute(); // Typical number on my box with Java 7: 1150 ms
}
}
|