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;
18
19/**
20 * How do the various schemes for iterating through a string compare?
21 */
22public class StringIterationBenchmark {
23    public void timeStringIteration0(int reps) {
24        String s = "hello, world!";
25        for (int rep = 0; rep < reps; ++rep) {
26            char ch;
27            for (int i = 0; i < s.length(); ++i) {
28                ch = s.charAt(i);
29            }
30        }
31    }
32    public void timeStringIteration1(int reps) {
33        String s = "hello, world!";
34        for (int rep = 0; rep < reps; ++rep) {
35            char ch;
36            for (int i = 0, length = s.length(); i < length; ++i) {
37                ch = s.charAt(i);
38            }
39        }
40    }
41    public void timeStringIteration2(int reps) {
42        String s = "hello, world!";
43        for (int rep = 0; rep < reps; ++rep) {
44            char ch;
45            char[] chars = s.toCharArray();
46            for (int i = 0, length = chars.length; i < length; ++i) {
47                ch = chars[i];
48            }
49        }
50    }
51    public void timeStringToCharArray(int reps) {
52        String s = "hello, world!";
53        for (int rep = 0; rep < reps; ++rep) {
54            char[] chars = s.toCharArray();
55        }
56    }
57}
58