Skip to content

Instantly share code, notes, and snippets.

@feilongjiang
Created August 20, 2021 02:08
Show Gist options
  • Save feilongjiang/1c3a6283e4d9eefea18f54b34b0df875 to your computer and use it in GitHub Desktop.
Save feilongjiang/1c3a6283e4d9eefea18f54b34b0df875 to your computer and use it in GitHub Desktop.
Benchmark
/*
* Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
* Copyright Huawei Technologies Co., Ltd. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.openjdk.bench.java.lang;
import java.util.Random;
import org.openjdk.jmh.annotations.*;
import java.util.concurrent.TimeUnit;
@State(Scope.Benchmark)
@Warmup(iterations = 5)
@Measurement(iterations = 5, time = 10, TimeUnit = TimeUnit.SECONDS)
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public class IndexOfBenchmark {
private static final Random rng = new Random(1999);
private static final int pathCnt = 1000;
private static final String [] latn1Len64 = new String[pathCnt];
static {
for (int i = 0; i < pathCnt; i++) {
latn1Len64[i] = makeRndString(false, 64);
}
}
private static String makeRndString(boolean isUtf16, int length) {
StringBuilder sb = new StringBuilder(length);
if(length > 0){
sb.append(isUtf16 ? '☺' : 'b');
for (int i = 1; i < length - 1; i++) {
sb.append((char)('b' + rng.nextInt(26)));
}
sb.append(rng.nextInt(3) >= 1 ? 'a' : 'b'); //66.6% of time 'a' is in string
}
return sb.toString();
}
@Benchmark
@Fork(value = 1, jvmArgs={"-XX:+AvoidUnalignedAccesses"})
public static void latin1Len0064Char() {
int ret = 0;
for (String what : latn1Len64) {
ret += what.indexOf('a');
}
}
@Benchmark
@Fork(value = 1, jvmArgs={"-XX:+AvoidUnalignedAccesses"})
public static void latin1Len0064CharWithIndex1() {
int ret = 0;
for (String what : latn1Len64) {
ret += what.indexOf('a', 1);
}
}
@Benchmark
@Fork(value = 1, jvmArgs={"-XX:+AvoidUnalignedAccesses"})
public static void latin1Len0064CharWithIndex2() {
int ret = 0;
for (String what : latn1Len64) {
ret += what.indexOf('a', 2);
}
}
@Benchmark
@Fork(value = 1, jvmArgs={"-XX:+AvoidUnalignedAccesses"})
public static void latin1Len0064CharWithIndex3() {
int ret = 0;
for (String what : latn1Len64) {
ret += what.indexOf('a', 3);
}
}
@Benchmark
@Fork(value = 1, jvmArgs={"-XX:+AvoidUnalignedAccesses"})
public static void latin1Len0064CharWithIndex4() {
int ret = 0;
for (String what : latn1Len64) {
ret += what.indexOf('a', 4);
}
}
@Benchmark
@Fork(value = 1, jvmArgs={"-XX:+AvoidUnalignedAccesses"})
public static void latin1Len0064CharWithIndex5() {
int ret = 0;
for (String what : latn1Len64) {
ret += what.indexOf('a', 5);
}
}
@Benchmark
@Fork(value = 1, jvmArgs={"-XX:+AvoidUnalignedAccesses"})
public static void latin1Len0064CharWithIndex6() {
int ret = 0;
for (String what : latn1Len64) {
ret += what.indexOf('a', 6);
}
}
@Benchmark
@Fork(value = 1, jvmArgs={"-XX:+AvoidUnalignedAccesses"})
public static void latin1Len0064CharWithIndex7() {
int ret = 0;
for (String what : latn1Len64) {
ret += what.indexOf('a', 7);
}
}
}
/*
* Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
* Copyright Huawei Technologies Co., Ltd. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.openjdk.bench.java.lang;
import java.util.Random;
import org.openjdk.jmh.annotations.*;
import java.util.concurrent.TimeUnit;
@State(Scope.Benchmark)
@Warmup(iterations = 5)
@Measurement(iterations = 5, time = 10, TimeUnit = TimeUnit.SECONDS)
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public class IndexOfBenchmark {
private static final Random rng = new Random(1999);
private static final int pathCnt = 1000;
private static final String [] latn1Len64 = new String[pathCnt];
static {
for (int i = 0; i < pathCnt; i++) {
latn1Len64[i] = makeRndString(false, 64);
}
}
private static String makeRndString(boolean isUtf16, int length) {
StringBuilder sb = new StringBuilder(length);
if(length > 0){
sb.append(isUtf16 ? '☺' : 'b');
for (int i = 1; i < length - 1; i++) {
sb.append((char)('b' + rng.nextInt(26)));
}
sb.append(rng.nextInt(3) >= 1 ? 'a' : 'b'); //66.6% of time 'a' is in string
}
return sb.toString();
}
@Benchmark
@Fork(value = 1, jvmArgs={"-XX:-AvoidUnalignedAccesses"})
public static void latin1Len0064Char() {
int ret = 0;
for (String what : latn1Len64) {
ret += what.indexOf('a');
}
}
@Benchmark
@Fork(value = 1, jvmArgs={"-XX:-AvoidUnalignedAccesses"})
public static void latin1Len0064CharWithIndex1() {
int ret = 0;
for (String what : latn1Len64) {
ret += what.indexOf('a', 1);
}
}
@Benchmark
@Fork(value = 1, jvmArgs={"-XX:-AvoidUnalignedAccesses"})
public static void latin1Len0064CharWithIndex2() {
int ret = 0;
for (String what : latn1Len64) {
ret += what.indexOf('a', 2);
}
}
@Benchmark
@Fork(value = 1, jvmArgs={"-XX:-AvoidUnalignedAccesses"})
public static void latin1Len0064CharWithIndex3() {
int ret = 0;
for (String what : latn1Len64) {
ret += what.indexOf('a', 3);
}
}
@Benchmark
@Fork(value = 1, jvmArgs={"-XX:-AvoidUnalignedAccesses"})
public static void latin1Len0064CharWithIndex4() {
int ret = 0;
for (String what : latn1Len64) {
ret += what.indexOf('a', 4);
}
}
@Benchmark
@Fork(value = 1, jvmArgs={"-XX:-AvoidUnalignedAccesses"})
public static void latin1Len0064CharWithIndex5() {
int ret = 0;
for (String what : latn1Len64) {
ret += what.indexOf('a', 5);
}
}
@Benchmark
@Fork(value = 1, jvmArgs={"-XX:-AvoidUnalignedAccesses"})
public static void latin1Len0064CharWithIndex6() {
int ret = 0;
for (String what : latn1Len64) {
ret += what.indexOf('a', 6);
}
}
@Benchmark
@Fork(value = 1, jvmArgs={"-XX:-AvoidUnalignedAccesses"})
public static void latin1Len0064CharWithIndex7() {
int ret = 0;
for (String what : latn1Len64) {
ret += what.indexOf('a', 7);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment