• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package examples;
18 
19 import static java.lang.Character.MIN_SURROGATE;
20 
21 import com.google.caliper.Benchmark;
22 import com.google.caliper.Param;
23 
24 /**
25  * Tests the performance of various StringBuilder methods.
26  */
27 public class StringBuilderBenchmark {
28 
29   @Param({"1", "10", "100"}) private int length;
30 
appendBoolean(int reps)31   @Benchmark void appendBoolean(int reps) {
32     StringBuilder sb = new StringBuilder();
33     for (int i = 0; i < reps; ++i) {
34       sb.setLength(0);
35       for (int j = 0; j < length; ++j) {
36         sb.append(true);
37         sb.append(false);
38       }
39     }
40   }
41 
appendChar(int reps)42   @Benchmark void appendChar(int reps) {
43     for (int i = 0; i < reps; ++i) {
44       StringBuilder sb = new StringBuilder();
45       for (int j = 0; j < length; ++j) {
46         sb.append('c');
47       }
48     }
49   }
50 
appendCharArray(int reps)51   @Benchmark void appendCharArray(int reps) {
52     char[] chars = "chars".toCharArray();
53     for (int i = 0; i < reps; ++i) {
54       StringBuilder sb = new StringBuilder();
55       for (int j = 0; j < length; ++j) {
56         sb.append(chars);
57       }
58     }
59   }
60 
appendCharSequence(int reps)61   @Benchmark void appendCharSequence(int reps) {
62     CharSequence cs = "chars";
63     for (int i = 0; i < reps; ++i) {
64       StringBuilder sb = new StringBuilder();
65       for (int j = 0; j < length; ++j) {
66         sb.append(cs);
67       }
68     }
69   }
70 
appendDouble(int reps)71   @Benchmark void appendDouble(int reps) {
72     double d = 1.2;
73     for (int i = 0; i < reps; ++i) {
74       StringBuilder sb = new StringBuilder();
75       for (int j = 0; j < length; ++j) {
76         sb.append(d);
77       }
78     }
79   }
80 
appendFloat(int reps)81   @Benchmark void appendFloat(int reps) {
82     float f = 1.2f;
83     for (int i = 0; i < reps; ++i) {
84       StringBuilder sb = new StringBuilder();
85       for (int j = 0; j < length; ++j) {
86         sb.append(f);
87       }
88     }
89   }
90 
appendInt(int reps)91   @Benchmark void appendInt(int reps) {
92     int n = 123;
93     for (int i = 0; i < reps; ++i) {
94       StringBuilder sb = new StringBuilder();
95       for (int j = 0; j < length; ++j) {
96         sb.append(n);
97       }
98     }
99   }
100 
appendLong(int reps)101   @Benchmark void appendLong(int reps) {
102     long l = 123;
103     for (int i = 0; i < reps; ++i) {
104       StringBuilder sb = new StringBuilder();
105       for (int j = 0; j < length; ++j) {
106         sb.append(l);
107       }
108     }
109   }
110 
appendObject(int reps)111   @Benchmark void appendObject(int reps) {
112     Object o = new Object();
113     for (int i = 0; i < reps; ++i) {
114       StringBuilder sb = new StringBuilder();
115       for (int j = 0; j < length; ++j) {
116         sb.append(o);
117       }
118     }
119   }
120 
appendString(int reps)121   @Benchmark void appendString(int reps) {
122     String s = "chars";
123     for (int i = 0; i < reps; ++i) {
124       StringBuilder sb = new StringBuilder();
125       for (int j = 0; j < length; ++j) {
126         sb.append(s);
127       }
128     }
129   }
130 
appendNull(int reps)131   @Benchmark void appendNull(int reps) {
132     StringBuilder sb = new StringBuilder();
133     for (int i = 0; i < reps; ++i) {
134       sb.setLength(0);
135       for (int j = 0; j < length; ++j) {
136         sb.append((String)null);
137         sb.append((StringBuilder)null);
138       }
139     }
140   }
141 
142   /** Times .reverse() when no surrogates are present. */
reverseNoSurrogates(int reps)143   @Benchmark void reverseNoSurrogates(int reps) {
144     final int length = Math.min(this.length, MIN_SURROGATE);
145     StringBuilder sb = new StringBuilder();
146     for (int j = 0; j < length; j++) {
147       sb.appendCodePoint(j);
148     }
149     for (int i = 0; i < reps; i++) {
150       for (int j = 0; j < 4; j++) {
151         sb.reverse();
152       }
153       if (sb.codePointAt(0) > MIN_SURROGATE)
154         throw new Error();
155     }
156   }
157 
158   /** Times .codePointAt(int) when no surrogates are present. */
codePointAtNoSurrogates(int reps)159   @Benchmark void codePointAtNoSurrogates(int reps) {
160     final int length = Math.min(this.length, MIN_SURROGATE);
161     StringBuilder sb = new StringBuilder();
162     for (int j = 0; j < length; j++) {
163       sb.appendCodePoint(j);
164     }
165     for (int i = 0; i < reps; i++) {
166       for (int j = 0; j < 4; j++) {
167         for (int k = 0; k < length - 1; k++) {
168           if (sb.codePointAt(k) > MIN_SURROGATE)
169             throw new Error();
170         }
171       }
172     }
173   }
174 
175   /** Times .codePointBefore(int) when no surrogates are present. */
codePointBeforeNoSurrogates(int reps)176   @Benchmark void codePointBeforeNoSurrogates(int reps) {
177     final int length = Math.min(this.length, MIN_SURROGATE);
178     StringBuilder sb = new StringBuilder();
179     for (int j = 0; j < length; j++) {
180       sb.appendCodePoint(j);
181     }
182     for (int i = 0; i < reps; i++) {
183       for (int j = 0; j < 4; j++) {
184         for (int k = 1; k < length; k++) {
185           if (sb.codePointBefore(k) > MIN_SURROGATE)
186             throw new Error();
187         }
188       }
189     }
190   }
191 }
192