Path: blob/master/test/micro/org/openjdk/bench/java/lang/StringEquals.java
41161 views
/*1* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22package org.openjdk.bench.java.lang;2324import org.openjdk.jmh.annotations.*;25import java.util.concurrent.TimeUnit;2627/*28* This benchmark naively explores String::equals performance29*/30@BenchmarkMode(Mode.AverageTime)31@OutputTimeUnit(TimeUnit.NANOSECONDS)32@State(Scope.Benchmark)33public class StringEquals {3435public String test = new String("0123456789");36public String test2 = new String("tgntogjnrognagronagroangroarngorngaorng");37public String test3 = new String(test); // equal to test, but not same38public String test4 = new String("0123\u01FF");39public String test5 = new String(test4); // equal to test4, but not same40public String test6 = new String("0123456780");41public String test7 = new String("0123\u01FE");4243@Benchmark44public boolean different() {45return test.equals(test2);46}4748@Benchmark49public boolean equal() {50return test.equals(test3);51}5253@Benchmark54public boolean almostEqual() {55return test.equals(test6);56}5758@Benchmark59public boolean almostEqualUTF16() {60return test4.equals(test7);61}6263@Benchmark64public boolean differentCoders() {65return test.equals(test4);66}6768@Benchmark69public boolean equalsUTF16() {70return test5.equals(test4);71}72}73747576