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
17package benchmarks.regression;
18
19import java.util.Formatter;
20import java.util.Locale;
21
22/**
23 * Compares Formatter against hand-written StringBuilder code.
24 */
25public class FormatterBenchmark {
26    public void timeFormatter_NoFormatting(int reps) {
27        for (int i = 0; i < reps; i++) {
28            Formatter f = new Formatter();
29            f.format("this is a reasonably short string that doesn't actually need any formatting");
30        }
31    }
32
33    public void timeStringBuilder_NoFormatting(int reps) {
34        for (int i = 0; i < reps; i++) {
35            StringBuilder sb = new StringBuilder();
36            sb.append("this is a reasonably short string that doesn't actually need any formatting");
37        }
38    }
39
40    public void timeFormatter_OneInt(int reps) {
41        Integer value = Integer.valueOf(1024); // We're not trying to benchmark boxing here.
42        for (int i = 0; i < reps; i++) {
43            Formatter f = new Formatter();
44            f.format("this is a reasonably short string that has an int %d in it", value);
45        }
46    }
47
48    public void timeFormatter_OneIntArabic(int reps) {
49        Locale arabic = new Locale("ar");
50        Integer value = Integer.valueOf(1024); // We're not trying to benchmark boxing here.
51        for (int i = 0; i < reps; i++) {
52            Formatter f = new Formatter();
53            f.format(arabic, "this is a reasonably short string that has an int %d in it", value);
54        }
55    }
56
57    public void timeStringBuilder_OneInt(int reps) {
58        for (int i = 0; i < reps; i++) {
59            StringBuilder sb = new StringBuilder();
60            sb.append("this is a reasonably short string that has an int ");
61            sb.append(1024);
62            sb.append(" in it");
63        }
64    }
65
66    public void timeFormatter_OneHexInt(int reps) {
67        Integer value = Integer.valueOf(1024); // We're not trying to benchmark boxing here.
68        for (int i = 0; i < reps; i++) {
69            Formatter f = new Formatter();
70            f.format("this is a reasonably short string that has an int %x in it", value);
71        }
72    }
73
74    public void timeStringBuilder_OneHexInt(int reps) {
75        for (int i = 0; i < reps; i++) {
76            StringBuilder sb = new StringBuilder();
77            sb.append("this is a reasonably short string that has an int ");
78            sb.append(Integer.toHexString(1024));
79            sb.append(" in it");
80        }
81    }
82
83    public void timeFormatter_OneFloat(int reps) {
84        Float value = Float.valueOf(10.24f); // We're not trying to benchmark boxing here.
85        for (int i = 0; i < reps; i++) {
86            Formatter f = new Formatter();
87            f.format("this is a reasonably short string that has a float %f in it", value);
88        }
89    }
90
91    public void timeFormatter_OneFloat_dot2f(int reps) {
92        Float value = Float.valueOf(10.24f); // We're not trying to benchmark boxing here.
93        for (int i = 0; i < reps; i++) {
94            Formatter f = new Formatter();
95            f.format("this is a reasonably short string that has a float %.2f in it", value);
96        }
97    }
98
99    public void timeFormatter_TwoFloats(int reps) {
100        Float value = Float.valueOf(10.24f); // We're not trying to benchmark boxing here.
101        for (int i = 0; i < reps; i++) {
102            Formatter f = new Formatter();
103            f.format("this is a reasonably short string that has two floats %f and %f in it", value, value);
104        }
105    }
106
107    public void timeStringBuilder_OneFloat(int reps) {
108        for (int i = 0; i < reps; i++) {
109            StringBuilder sb = new StringBuilder();
110            sb.append("this is a reasonably short string that has a float ");
111            sb.append(10.24f);
112            sb.append(" in it");
113        }
114    }
115
116    public void timeFormatter_OneString(int reps) {
117        for (int i = 0; i < reps; i++) {
118            Formatter f = new Formatter();
119            f.format("this is a reasonably short string that has a string %s in it", "hello");
120        }
121    }
122
123    public void timeStringBuilder_OneString(int reps) {
124        for (int i = 0; i < reps; i++) {
125            StringBuilder sb = new StringBuilder();
126            sb.append("this is a reasonably short string that has a string ");
127            sb.append("hello");
128            sb.append(" in it");
129        }
130    }
131}
132