mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-27 23:04:50 +02:00
8271840: Add simple Integer.toString microbenchmarks
Reviewed-by: shade
This commit is contained in:
parent
18dd4d469d
commit
55bd52a142
2 changed files with 40 additions and 10 deletions
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2014, 2021, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
@ -24,36 +24,48 @@ package org.openjdk.bench.java.lang;
|
||||||
|
|
||||||
import org.openjdk.jmh.annotations.Benchmark;
|
import org.openjdk.jmh.annotations.Benchmark;
|
||||||
import org.openjdk.jmh.annotations.BenchmarkMode;
|
import org.openjdk.jmh.annotations.BenchmarkMode;
|
||||||
|
import org.openjdk.jmh.annotations.Fork;
|
||||||
|
import org.openjdk.jmh.annotations.Measurement;
|
||||||
import org.openjdk.jmh.annotations.Mode;
|
import org.openjdk.jmh.annotations.Mode;
|
||||||
import org.openjdk.jmh.annotations.OutputTimeUnit;
|
import org.openjdk.jmh.annotations.OutputTimeUnit;
|
||||||
import org.openjdk.jmh.annotations.Param;
|
import org.openjdk.jmh.annotations.Param;
|
||||||
import org.openjdk.jmh.annotations.Scope;
|
import org.openjdk.jmh.annotations.Scope;
|
||||||
import org.openjdk.jmh.annotations.Setup;
|
import org.openjdk.jmh.annotations.Setup;
|
||||||
import org.openjdk.jmh.annotations.State;
|
import org.openjdk.jmh.annotations.State;
|
||||||
|
import org.openjdk.jmh.annotations.Warmup;
|
||||||
import org.openjdk.jmh.infra.Blackhole;
|
import org.openjdk.jmh.infra.Blackhole;
|
||||||
|
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests java.lang.Integer
|
* Test various java.lang.Integer operations
|
||||||
*/
|
*/
|
||||||
@BenchmarkMode(Mode.AverageTime)
|
@BenchmarkMode(Mode.AverageTime)
|
||||||
@OutputTimeUnit(TimeUnit.MICROSECONDS)
|
@OutputTimeUnit(TimeUnit.MICROSECONDS)
|
||||||
@State(Scope.Thread)
|
@State(Scope.Thread)
|
||||||
|
@Warmup(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
|
||||||
|
@Measurement(iterations = 5, time = 1000, timeUnit = TimeUnit.MILLISECONDS)
|
||||||
|
@Fork(3)
|
||||||
public class Integers {
|
public class Integers {
|
||||||
|
|
||||||
@Param("500")
|
@Param("500")
|
||||||
private int size;
|
private int size;
|
||||||
|
|
||||||
private String[] strings;
|
private String[] strings;
|
||||||
|
private int[] intsSmall;
|
||||||
|
private int[] intsBig;
|
||||||
|
|
||||||
@Setup
|
@Setup
|
||||||
public void setup() {
|
public void setup() {
|
||||||
Random r = new Random(0);
|
Random r = new Random(0);
|
||||||
strings = new String[size];
|
strings = new String[size];
|
||||||
|
intsSmall = new int[size];
|
||||||
|
intsBig = new int[size];
|
||||||
for (int i = 0; i < size; i++) {
|
for (int i = 0; i < size; i++) {
|
||||||
strings[i] = "" + (r.nextInt(10000) - 5000);
|
strings[i] = "" + (r.nextInt(10000) - (5000));
|
||||||
|
intsSmall[i] = 100 * i + i + 103;
|
||||||
|
intsBig[i] = ((100 * i + i) << 24) + 4543 + i * 4;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -63,4 +75,20 @@ public class Integers {
|
||||||
bh.consume(Integer.parseInt(s));
|
bh.consume(Integer.parseInt(s));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Performs toString on small values, just a couple of digits. */
|
||||||
|
@Benchmark
|
||||||
|
public void toStringSmall(Blackhole bh) {
|
||||||
|
for (int i : intsSmall) {
|
||||||
|
bh.consume(Integer.toString(i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Performs toString on large values, roughly 10 digits. */
|
||||||
|
@Benchmark
|
||||||
|
public void toStringBig(Blackhole bh) {
|
||||||
|
for (int i : intsBig) {
|
||||||
|
bh.consume(Integer.toString(i));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2014, 2021, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
@ -24,13 +24,15 @@ package org.openjdk.bench.java.lang;
|
||||||
|
|
||||||
import org.openjdk.jmh.annotations.Benchmark;
|
import org.openjdk.jmh.annotations.Benchmark;
|
||||||
import org.openjdk.jmh.annotations.BenchmarkMode;
|
import org.openjdk.jmh.annotations.BenchmarkMode;
|
||||||
|
import org.openjdk.jmh.annotations.Fork;
|
||||||
|
import org.openjdk.jmh.annotations.Measurement;
|
||||||
import org.openjdk.jmh.annotations.Mode;
|
import org.openjdk.jmh.annotations.Mode;
|
||||||
import org.openjdk.jmh.annotations.OutputTimeUnit;
|
import org.openjdk.jmh.annotations.OutputTimeUnit;
|
||||||
import org.openjdk.jmh.annotations.Param;
|
import org.openjdk.jmh.annotations.Param;
|
||||||
import org.openjdk.jmh.annotations.Scope;
|
import org.openjdk.jmh.annotations.Scope;
|
||||||
import org.openjdk.jmh.annotations.Setup;
|
import org.openjdk.jmh.annotations.Setup;
|
||||||
import org.openjdk.jmh.annotations.State;
|
import org.openjdk.jmh.annotations.State;
|
||||||
import org.openjdk.jmh.annotations.Threads;
|
import org.openjdk.jmh.annotations.Warmup;
|
||||||
import org.openjdk.jmh.infra.Blackhole;
|
import org.openjdk.jmh.infra.Blackhole;
|
||||||
|
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
@ -38,6 +40,9 @@ import java.util.concurrent.TimeUnit;
|
||||||
@BenchmarkMode(Mode.AverageTime)
|
@BenchmarkMode(Mode.AverageTime)
|
||||||
@OutputTimeUnit(TimeUnit.MICROSECONDS)
|
@OutputTimeUnit(TimeUnit.MICROSECONDS)
|
||||||
@State(Scope.Thread)
|
@State(Scope.Thread)
|
||||||
|
@Warmup(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
|
||||||
|
@Measurement(iterations = 5, time = 1000, timeUnit = TimeUnit.MILLISECONDS)
|
||||||
|
@Fork(3)
|
||||||
public class Longs {
|
public class Longs {
|
||||||
|
|
||||||
@Param("500")
|
@Param("500")
|
||||||
|
@ -56,18 +61,16 @@ public class Longs {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Performs toString on a bunch of java.lang.Long:s, all with small values, just a couple of digits. */
|
/** Performs toString on small values, just a couple of digits. */
|
||||||
@Benchmark
|
@Benchmark
|
||||||
@Threads(Threads.MAX)
|
|
||||||
public void toStringSmall(Blackhole bh) {
|
public void toStringSmall(Blackhole bh) {
|
||||||
for (long value : longArraySmall) {
|
for (long value : longArraySmall) {
|
||||||
bh.consume(Long.toString(value));
|
bh.consume(Long.toString(value));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Performs toString on a bunch of java.lang.Long:s, all with large values, around 10 digits. */
|
/** Performs toString on large values, around 10 digits. */
|
||||||
@Benchmark
|
@Benchmark
|
||||||
@Threads(Threads.MAX)
|
|
||||||
public void toStringBig(Blackhole bh) {
|
public void toStringBig(Blackhole bh) {
|
||||||
for (long value : longArrayBig) {
|
for (long value : longArrayBig) {
|
||||||
bh.consume(Long.toString(value));
|
bh.consume(Long.toString(value));
|
||||||
|
@ -80,7 +83,6 @@ public class Longs {
|
||||||
public int innerLoops = 1500;
|
public int innerLoops = 1500;
|
||||||
|
|
||||||
@Benchmark
|
@Benchmark
|
||||||
@Threads(Threads.MAX)
|
|
||||||
public long repetitiveSubtraction() {
|
public long repetitiveSubtraction() {
|
||||||
long x = 127, dx = 0;
|
long x = 127, dx = 0;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue