1 /*
   2  * Copyright (c) 2019, 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 
  24 package org.openjdk.bench.java.lang;
  25 
  26 import org.openjdk.jmh.annotations.*;
  27 import java.util.concurrent.TimeUnit;
  28 
  29 @BenchmarkMode(Mode.AverageTime)
  30 @OutputTimeUnit(TimeUnit.NANOSECONDS)
  31 @State(Scope.Benchmark)
  32 public class StringReplace {
  33 
  34     public String slat1 = new String("java.lang.String");
  35     public String sutf16 = new String("\u1D36\u1D43\u1D5B\u1D43 \uFF11\uFF13");
  36 
  37     @Benchmark
  38     public String replace0x1_1_Latin1() {
  39         return slat1.replace("Z", "z");
  40     }
  41 
  42     @Benchmark
  43     public String replace1x1_0_Latin1() {
  44         return slat1.replace("g", "");
  45     }
  46 
  47     @Benchmark
  48     public String replace1x1_1_Latin1() {
  49         return slat1.replace("g", "k");
  50     }
  51 
  52     @Benchmark
  53     public String replace1x1_2_Latin1() {
  54         return slat1.replace("g", "th");
  55     }
  56 
  57     @Benchmark
  58     public String replace2x1_0_Latin1() {
  59         return slat1.replace(".", "");
  60     }
  61 
  62     @Benchmark
  63     public String replace2x1_1_Latin1() {
  64         return slat1.replace(".", "/");
  65     }
  66 
  67     @Benchmark
  68     public String replace2x1_2_Latin1() {
  69         return slat1.replace(".", "::");
  70     }
  71 
  72     @Benchmark
  73     public String replace0x1_1_UTF16() {
  74         return sutf16.replace("\u1D36", "\u1D38\u1D3A");
  75     }
  76 
  77     @Benchmark
  78     public String replace1x1_0_UTF16() {
  79         return sutf16.replace("\uFF11", "");
  80     }
  81 
  82     @Benchmark
  83     public String replace1x1_1_UTF16() {
  84         return sutf16.replace("\u1D36", "\u24BF");
  85     }
  86 
  87     @Benchmark
  88     public String replace1x1_2_UTF16() {
  89         return sutf16.replace("\uFF11", "\uFF11\uFF12");
  90     }
  91 
  92     @Benchmark
  93     public String replace2x1_0_UTF16() {
  94         return sutf16.replace("\u1D43", "");
  95     }
  96 
  97     @Benchmark
  98     public String replace2x1_1_UTF16() {
  99         return sutf16.replace("\u1D43", "\u1D2C");
 100     }
 101 
 102     @Benchmark
 103     public String replace2x1_2_UTF16() {
 104         return sutf16.replace("\u1D43", "\u21DB\u21DB");
 105     }
 106 }