summaryrefslogtreecommitdiff
path: root/BenchmarkFramework/app/src/main/java/art_benchmarks/micro/StringEquals.java
blob: 012c92c9ebfe58cd641ef74f46a674f994f7dc7f (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
/*
 * Copyright (C) 2015 Linaro Limited. All rights reserved.
 *
 * 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.
 *
 */

/*
 * Description:     Tracks performance of String.equals when working with
 *                  cache-limiting amounts of character data. Some prefetch
 *                  hint investigations have shown a performance gain here.
 * Main Focus:      Comparison of many, very large (re: L1 cache) strings.
 * Secondary Focus:
 *
 */

/*
 * TODO:            Test various sizes of strings.
 * TODO:            Benchmark other cases, like comparing strings of different
 *                  sizes together (probably the most command case), or similar
 *                  strings that have a run of equal characters to begin with.
 */

package art_benchmarks.micro;

import java.lang.StringBuilder;
import java.util.Random;

public class StringEquals {
  private static Random rnd = new Random(0);
  private static final int NUM_Equals = 1024;
  private static final int STR_Length = 512;
  private static String[] randomStrings = new String[NUM_Equals];
  private static Boolean[] equalsResults = new Boolean[NUM_Equals - 1];

  static {
    generateRandomStrings();
  }

  private static void generateRandomStrings() {
    for (int i = 0; i < NUM_Equals; i++) {
      StringBuilder sb = new StringBuilder();
      for (int j = 0; j < STR_Length; j++) {
        sb.append(Character.valueOf((char)(rnd.nextInt(25) + 65)));
      }
      randomStrings[i] = sb.toString();
    }
  }

  public void timeEquals(int iterations) {
    for (int i = 0; i < iterations; i++) {
      for (int j = 0; j < NUM_Equals - 1; j++) {
        equalsResults[j] = randomStrings[j].equals(randomStrings[j + 1]);
      }
    }
  }

  public static void main(String[] args) {
    StringEquals eq = new StringEquals();
    long before = System.currentTimeMillis();
    eq.timeEquals(1500);
    eq.timeEquals(1500);
    long after = System.currentTimeMillis();
    System.out.println("art_benchmarks/micro/StringEquals: " + (after - before));
  }
}