1 /*
   2  * Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.openjdk.bench.java.lang;
  24 
  25 import org.openjdk.jmh.annotations.Benchmark;
  26 import org.openjdk.jmh.annotations.BenchmarkMode;
  27 import org.openjdk.jmh.annotations.Mode;
  28 import org.openjdk.jmh.annotations.OutputTimeUnit;
  29 import org.openjdk.jmh.annotations.Scope;
  30 import org.openjdk.jmh.annotations.Setup;
  31 import org.openjdk.jmh.annotations.State;
  32 
  33 import java.util.concurrent.TimeUnit;
  34 
  35 @BenchmarkMode(Mode.AverageTime)
  36 @OutputTimeUnit(TimeUnit.NANOSECONDS)
  37 @State(Scope.Thread)
  38 public class StringIndexOf {
  39 
  40     private String dataString;
  41     private String searchString;
  42     private String dataStringBig;
  43     private String searchStringBig;
  44     private String data;
  45     private String sub;
  46     private String shortSub1;
  47     private String data2;
  48     private String shortSub2;
  49 
  50     @Setup
  51     public void setup() {
  52         dataString = "ngdfilsoscargfdgf";
  53         searchString = "oscar";
  54         dataStringBig = "2937489745890797905764956790452976742965790437698498409583479067ngdcapaapapapasdkajdlkajskldjaslkjdlkasjdsalkjas";
  55         searchStringBig = "capaapapapasdkajdlkajskldjaslkjdlkasjdsalk";
  56         data = "0000100101010010110101010010101110101001110110101010010101010010000010111010101010101010100010010101110111010101101010100010010100001010111111100001010101001010100001010101001010101010111010010101010101010101010101010";
  57         sub = "10101010";
  58         shortSub1 = "1";
  59         data2 = "00001001010100a10110101010010101110101001110110101010010101010010000010111010101010101010a100010010101110111010101101010100010010a100a0010101111111000010101010010101000010101010010101010101110a10010101010101010101010101010";
  60         shortSub2 = "a";
  61     }
  62 
  63     /** IndexOf Micros Strings */
  64 
  65     /**
  66      * Benchmarks String.indexOf with a rather small String to search and a rather small String to search for. The
  67      * searched string contains the string that is searched for.
  68      */
  69     @Benchmark
  70     public int success() {
  71         return dataString.indexOf(searchString, 2);
  72     }
  73 
  74     /**
  75      * Benchmarks String.indexOf with a rather big String to search and a rather big String to search for. The searched
  76      * string contains the string that is searched for.
  77      */
  78     @Benchmark
  79     public int successBig() {
  80         return dataStringBig.indexOf(searchStringBig, 2);
  81     }
  82 
  83     /**
  84      * Benchmarks String.indexOf with a rather big String. Search repeatedly for a matched that is 8 chars and most
  85      * oftenly will require a inner lopp match in String.indexOf with sse42.
  86      */
  87     @Benchmark
  88     public int advancedWithMediumSub() {
  89         int index = 0;
  90         int dummy = 0;
  91         while ((index = data.indexOf(sub, index)) > -1) {
  92             index++;
  93             dummy += index;
  94         }
  95         return dummy;
  96     }
  97 
  98 
  99     /**
 100      * Benchmarks String.indexOf with a rather big String. Search repeatedly for a matched that is 1 chars will find a
 101      * huge amount of matches
 102      */
 103     @Benchmark
 104     public int advancedWithShortSub1() {
 105         int dummy = 0;
 106         int index = 0;
 107         while ((index = data.indexOf(shortSub1, index)) > -1) {
 108             index++;
 109             dummy += index;
 110         }
 111         return dummy;
 112     }
 113 
 114 
 115     /**
 116      * Benchmarks String.indexOf with a rather big String. Search repeatedly for a matched that is 1 chars but only with
 117      * a few matches.
 118      */
 119     @Benchmark
 120     public int advancedWithShortSub2() {
 121         int dummy = 0;
 122         int index = 0;
 123         while ((index = data2.indexOf(shortSub2, index)) > -1) {
 124             index++;
 125             dummy += index;
 126         }
 127         return dummy;
 128     }
 129 
 130     @Benchmark
 131     public void constantPattern() {
 132         String tmp = "simple-hash:SHA-1/UTF-8";
 133         if (!tmp.contains("SHA-1")) {
 134             throw new RuntimeException("indexOf failed");
 135         }
 136     }
 137 
 138 }