1/*
2 * Copyright (C) 2011 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.Locale;
20import com.google.caliper.Param;
21import com.google.caliper.SimpleBenchmark;
22
23public class StringCaseMappingBenchmark extends SimpleBenchmark {
24    enum Inputs {
25        EMPTY(""),
26
27        // TODO: include hairy inputs like turkish and greek.
28        // TODO: locale makes a difference too.
29
30        LOWER2(lower(2)),
31        UPPER2(upper(2)),
32        MIXED2(mixed(2)),
33
34        LOWER8(lower(8)),
35        UPPER8(upper(8)),
36        MIXED8(mixed(8)),
37
38        LOWER32(lower(32)),
39        UPPER32(upper(32)),
40        MIXED32(mixed(32)),
41
42        LOWER512(lower(512)),
43        UPPER512(upper(512)),
44        MIXED512(mixed(512)),
45
46        LOWER2048(lower(2048)),
47        UPPER2048(upper(2048)),
48        MIXED2048(mixed(2048)),
49
50        LOWER_1M(lower(1024*1024)),
51        UPPER_1M(upper(1024*1024)),
52        MIXED_1M(mixed(1024*1024));
53
54        final String value;
55        private Inputs(String value) { this.value = value; }
56        private static String lower(int length) {
57            return makeString(length, "a0b1c2d3e4f5g6h7i8j9klmnopqrstuvwxyz");
58        }
59        private static String upper(int length) {
60            return makeString(length, "A0B1C2D3E4F5G6H7I8J9KLMNOPQRSTUVWXYZ");
61        }
62        private static String mixed(int length) {
63            return makeString(length, "Aa0Bb1Cc2Dd3Ee4Ff5Gg6Hh7Ii8Jj9KkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz");
64        }
65        private static String makeString(int length, String alphabet) {
66            StringBuilder sb = new StringBuilder(length);
67            for (int i = 0; i < length; ++i) {
68                sb.append(alphabet.charAt(i % alphabet.length()));
69            }
70            return sb.toString();
71        }
72    }
73    @Param private Inputs s;
74
75    public void timeToUpperCase_US(int reps) {
76        for (int i = 0; i < reps; ++i) {
77            s.value.toUpperCase(Locale.US);
78        }
79    }
80
81    // toUpperCase for Greek is an extra-hard case that uses icu4c's Transliterator.
82    public void timeToUpperCase_el_GR(int reps) {
83        Locale el_GR = new Locale("el", "GR");
84        for (int i = 0; i < reps; ++i) {
85            s.value.toUpperCase(el_GR);
86        }
87    }
88
89    public void timeToLowerCase_US(int reps) {
90        for (int i = 0; i < reps; ++i) {
91            s.value.toUpperCase(Locale.US);
92        }
93    }
94
95    public void timeToUpperCase_Ascii(int reps) {
96        for (int i = 0; i < reps; ++i) {
97            toUpperCaseAscii(s.value);
98        }
99    }
100
101    public void timeToLowerCase_Ascii(int reps) {
102        for (int i = 0; i < reps; ++i) {
103            toUpperCaseAscii(s.value);
104        }
105    }
106
107    public void timeToUpperCase_ICU(int reps) {
108        for (int i = 0; i < reps; ++i) {
109            libcore.icu.ICU.toUpperCase(s.value, Locale.US);
110        }
111    }
112
113    public void timeToLowerCase_ICU(int reps) {
114        for (int i = 0; i < reps; ++i) {
115            libcore.icu.ICU.toLowerCase(s.value, Locale.US);
116        }
117    }
118
119    public static String toUpperCaseAscii(String s) {
120        for (int i = 0, length = s.length(); i < length; i++) {
121            char c = s.charAt(i);
122            if (c < 'a' || c > 'z') {
123                continue; // fast path avoids allocation
124            }
125
126            // slow path: s contains lower case chars
127            char[] result = s.toCharArray();
128            for (; i < length; i++) {
129                c = result[i];
130                if (c >= 'a' && c <= 'z') {
131                    result[i] -= ('a' - 'A');
132                }
133            }
134            return new String(result);
135        }
136        return s;
137    }
138
139    public static String toLowerCaseAscii(String s) {
140        for (int i = 0, length = s.length(); i < length; i++) {
141            char c = s.charAt(i);
142            if (c < 'A' || c > 'Z') {
143                continue; // fast path avoids allocation
144            }
145
146            // slow path: s contains upper case chars
147            char[] result = s.toCharArray();
148            for (; i < length; i++) {
149                c = result[i];
150                if (c >= 'A' && c <= 'Z') {
151                    result[i] += ('a' - 'A');
152                }
153            }
154            return new String(result);
155        }
156        return s;
157    }
158}
159