Path: blob/master/test/micro/org/openjdk/bench/java/lang/StringConcat.java
41161 views
/*1* Copyright (c) 2018, 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.Benchmark;25import org.openjdk.jmh.annotations.BenchmarkMode;26import org.openjdk.jmh.annotations.Fork;27import org.openjdk.jmh.annotations.Measurement;28import org.openjdk.jmh.annotations.Mode;29import org.openjdk.jmh.annotations.OutputTimeUnit;30import org.openjdk.jmh.annotations.Param;31import org.openjdk.jmh.annotations.Scope;32import org.openjdk.jmh.annotations.State;33import org.openjdk.jmh.annotations.Warmup;3435import java.util.concurrent.TimeUnit;3637/**38* Trivial String concatenation benchmark.39*/40@BenchmarkMode(Mode.AverageTime)41@OutputTimeUnit(TimeUnit.NANOSECONDS)42@State(Scope.Thread)43@Warmup(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)44@Measurement(iterations = 5, time = 1000, timeUnit = TimeUnit.MILLISECONDS)45@Fork(3)46public class StringConcat {4748@Param("4711")49public int intValue;5051public String stringValue = String.valueOf(intValue);5253public Object objectValue = Long.valueOf(intValue);5455public boolean boolValue = true;5657public byte byteValue = (byte)-128;5859public String emptyString = "";6061@Benchmark62public String concatConstInt() {63return "string" + intValue;64}6566@Benchmark67public String concatConstString() {68return "string" + stringValue;69}7071@Benchmark72public String concatEmptyRight() {73return stringValue + emptyString;74}7576@Benchmark77public String concatEmptyLeft() {78return emptyString + stringValue;79}8081@Benchmark82public String concatEmptyConstInt() {83return "" + intValue;84}8586@Benchmark87public String concatEmptyConstString() {88return "" + stringValue;89}9091@Benchmark92public String concatMethodConstString() {93return "string".concat(stringValue);94}9596@Benchmark97public String concatConstIntConstInt() {98return "string" + intValue + "string" + intValue;99}100101@Benchmark102public String concatConstStringConstInt() {103return "string" + stringValue + "string" + intValue;104}105106@Benchmark107public String concatMix4String() {108// Investigate "profile pollution" between shared LFs that might eliminate some JIT optimizations109String s1 = "string" + stringValue + stringValue + stringValue + stringValue;110String s2 = "string" + stringValue + "string" + stringValue + stringValue + stringValue;111String s3 = stringValue + stringValue + "string" + stringValue + "string" + stringValue + "string";112String s4 = "string" + stringValue + "string" + stringValue + "string" + stringValue + "string" + stringValue + "string";113return s1 + s2 + s3 + s4;114}115116@Benchmark117public String concatConst4String() {118return "string" + stringValue + stringValue + stringValue + stringValue;119}120121@Benchmark122public String concat4String() {123return stringValue + stringValue + stringValue + stringValue;124}125126@Benchmark127public String concatConst2String() {128return "string" + stringValue + stringValue;129}130131@Benchmark132public String concatConstBoolByte() {133return "string" + boolValue + byteValue;134}135136@Benchmark137public String concatConst6String() {138return "string" + stringValue + stringValue + stringValue + stringValue + stringValue + stringValue;139}140141@Benchmark142public String concat6String() {143return stringValue + stringValue + stringValue + stringValue + stringValue + stringValue;144}145146@Benchmark147public String concatConst6Object() {148return "string" + objectValue + objectValue + objectValue + objectValue + objectValue + objectValue;149}150151}152153154