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 import org.openjdk.jmh.infra.Blackhole;
  33 
  34 import java.util.Random;
  35 import java.util.concurrent.TimeUnit;
  36 
  37 @BenchmarkMode(Mode.AverageTime)
  38 @OutputTimeUnit(TimeUnit.NANOSECONDS)
  39 @State(Scope.Thread)
  40 public class StringOther {
  41 
  42     private String testString;
  43     private Random rnd;
  44 
  45     private String str1, str2, str3, str4;
  46 
  47     @Setup
  48     public void setup() {
  49         testString = "Idealism is what precedes experience; cynicism is what follows.";
  50         str1 = "vm-guld vm-guld vm-guld";
  51         str2 = "vm-guld vm-guld vm-guldx";
  52         str3 = "vm-guld vm-guld vm-guldx";
  53         str4 = "adadaskasdjierudks";
  54         rnd = new Random();
  55     }
  56 
  57     @Benchmark
  58     public void charAt(Blackhole bh) {
  59         for (int i = 0; i < testString.length(); i++) {
  60             bh.consume(testString.charAt(i));
  61         }
  62     }
  63 
  64     @Benchmark
  65     public int compareTo() {
  66         int total = 0;
  67         total += str1.compareTo(str2);
  68         total += str2.compareTo(str3);
  69         total += str3.compareTo(str4);
  70         return total;
  71     }
  72 
  73     /**
  74      * Creates (hopefully) unique Strings and internizes them, creating a zillion forgettable strings in the JVMs string
  75      * pool.
  76      * <p/>
  77      * This will test 1.) The data structure/whatever for getting and adding Strings to intern table. 2.) The
  78      * intern-caches (java) behaviour on negative lookup (the string is new) 3.) GC's handling of weak handles. Since
  79      * every gc we must process and pretty much kill a zillion interned strings that are now not referenced anymore, the
  80      * majority of GC time will be spent in handle processing. So we get a picture of how well the pathological case of
  81      * this goes.
  82      */
  83     @Benchmark
  84     public String internUnique() {
  85         return String.valueOf(rnd.nextInt()).intern();
  86     }
  87 
  88 }