RealToStringBenchmark.java revision 5a7833b406bb2716b057d3ed923f22f1f86b2a20
1/*
2 * Copyright (C) 2010 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 com.google.caliper.Param;
20import com.google.caliper.Runner;
21import com.google.caliper.SimpleBenchmark;
22
23public class RealToStringBenchmark extends SimpleBenchmark {
24
25    private static final float SMALL  = -123.45f;
26    private static final float MEDIUM = -123.45e8f;
27    private static final float LARGE  = -123.45e36f;
28
29    public void timeFloat_toString_NaN(int reps) {
30        for (int rep = 0; rep < reps; ++rep) {
31            Float.toString(Float.NaN);
32        }
33    }
34
35    public void timeFloat_toString_NEGATIVE_INFINITY(int reps) {
36        for (int rep = 0; rep < reps; ++rep) {
37            Float.toString(Float.NEGATIVE_INFINITY);
38        }
39    }
40
41    public void timeFloat_toString_POSITIVE_INFINITY(int reps) {
42        for (int rep = 0; rep < reps; ++rep) {
43            Float.toString(Float.POSITIVE_INFINITY);
44        }
45    }
46
47    public void timeFloat_toString_zero(int reps) {
48        for (int rep = 0; rep < reps; ++rep) {
49            Float.toString(0.0f);
50        }
51    }
52
53    public void timeFloat_toString_minusZero(int reps) {
54        for (int rep = 0; rep < reps; ++rep) {
55            Float.toString(-0.0f);
56        }
57    }
58
59    public void timeFloat_toString_small(int reps) {
60        for (int rep = 0; rep < reps; ++rep) {
61            Float.toString(SMALL);
62        }
63    }
64
65    public void timeFloat_toString_medium(int reps) {
66        for (int rep = 0; rep < reps; ++rep) {
67            Float.toString(MEDIUM);
68        }
69    }
70
71    public void timeFloat_toString_large(int reps) {
72        for (int rep = 0; rep < reps; ++rep) {
73            Float.toString(LARGE);
74        }
75    }
76
77    public void timeStringBuilder_small(int reps) {
78        for (int rep = 0; rep < reps; ++rep) {
79            new StringBuilder().append(SMALL);
80        }
81    }
82
83    public void timeStringBuilder_medium(int reps) {
84        for (int rep = 0; rep < reps; ++rep) {
85            new StringBuilder().append(MEDIUM);
86        }
87    }
88
89    public void timeStringBuilder_large(int reps) {
90        for (int rep = 0; rep < reps; ++rep) {
91            new StringBuilder().append(LARGE);
92        }
93    }
94
95    public void timeFormatter_small(int reps) {
96        for (int rep = 0; rep < reps; ++rep) {
97            String.format("%f", SMALL);
98        }
99    }
100
101    public void timeFormatter_medium(int reps) {
102        for (int rep = 0; rep < reps; ++rep) {
103            String.format("%f", MEDIUM);
104        }
105    }
106
107    public void timeFormatter_large(int reps) {
108        for (int rep = 0; rep < reps; ++rep) {
109            String.format("%f", LARGE);
110        }
111    }
112
113    public void timeFormatter_dot2f_small(int reps) {
114        for (int rep = 0; rep < reps; ++rep) {
115            String.format("%.2f", SMALL);
116        }
117    }
118
119    public void timeFormatter_dot2f_medium(int reps) {
120        for (int rep = 0; rep < reps; ++rep) {
121            String.format("%.2f", MEDIUM);
122        }
123    }
124
125    public void timeFormatter_dot2f_large(int reps) {
126        for (int rep = 0; rep < reps; ++rep) {
127            String.format("%.2f", LARGE);
128        }
129    }
130}
131