aboutsummaryrefslogtreecommitdiffstats
path: root/guava-tests/benchmark/com/google/common/base/AsciiBenchmark.java
blob: fa92d40ef548d22cafb1358fc2025b280cd9f2cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
 * Copyright (C) 2010 The Guava Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.common.base;

import com.google.caliper.Param;
import com.google.caliper.Runner;
import com.google.caliper.SimpleBenchmark;
import com.google.common.base.Ascii;
import com.google.common.collect.Lists;
import com.google.common.primitives.Chars;

import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Random;

/**
 * Benchmarks for the ASCII class.
 *
 * @author Kevin Bourrillion
 */
public class AsciiBenchmark extends SimpleBenchmark {
  private static String ALPHA =
      "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  private static String NONALPHA =
      "0123456789`~-_=+[]{}|;:',.<>/?!@#$%^&*()\"\\";

  @Param({"20", "2000"}) int size;
  @Param({"2", "20"}) int nonAlphaRatio; // one non-alpha char per this many chars
  @Param boolean noWorkToDo;

  Random random;
  String testString;

  @Override protected void setUp() {
    random = new Random();

    int nonAlpha = size / nonAlphaRatio;
    int alpha = size - nonAlpha;

    List<Character> chars = Lists.newArrayListWithCapacity(size);
    for (int i = 0; i < alpha; i++) {
      chars.add(randomAlpha());
    }
    for (int i = 0; i < nonAlpha; i++) {
      chars.add(randomNonAlpha());
    }
    Collections.shuffle(chars, random);
    char[] array = Chars.toArray(chars);
    this.testString = new String(array);
  }

  private char randomAlpha() {
    return ALPHA.charAt(random.nextInt(ALPHA.length()));
  }

  private char randomNonAlpha() {
    return NONALPHA.charAt(random.nextInt(NONALPHA.length()));
  }

  public int timeAsciiToUpperCase(int reps) {
    String string = noWorkToDo
        ? Ascii.toUpperCase(testString)
        : testString;

    int dummy = 0;
    for (int i = 0; i < reps; i++) {
      dummy += Ascii.toUpperCase(string).length();
    }
    return dummy;
  }

  public int timeStringToUpperCase1(int reps) {
    String string = noWorkToDo
        ? testString.toUpperCase()
        : testString;

    int dummy = 0;
    for (int i = 0; i < reps; i++) {
      dummy += string.toUpperCase().length();
    }
    return dummy;
  }

  public int timeStringToUpperCase2(int reps) {
    String string = noWorkToDo
        ? testString.toUpperCase(Locale.US)
        : testString;

    int dummy = 0;
    for (int i = 0; i < reps; i++) {
      dummy += string.toUpperCase(Locale.US).length();
    }
    return dummy;
  }

  public static void main(String[] args) {
    Runner.main(AsciiBenchmark.class, args);
  }
}