Skip to content

Commit 55bd52a

Browse files
committed
8271840: Add simple Integer.toString microbenchmarks
Reviewed-by: shade
1 parent 18dd4d4 commit 55bd52a

2 files changed

Lines changed: 40 additions & 10 deletions

File tree

test/micro/org/openjdk/bench/java/lang/Integers.java

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2014, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -24,36 +24,48 @@
2424

2525
import org.openjdk.jmh.annotations.Benchmark;
2626
import org.openjdk.jmh.annotations.BenchmarkMode;
27+
import org.openjdk.jmh.annotations.Fork;
28+
import org.openjdk.jmh.annotations.Measurement;
2729
import org.openjdk.jmh.annotations.Mode;
2830
import org.openjdk.jmh.annotations.OutputTimeUnit;
2931
import org.openjdk.jmh.annotations.Param;
3032
import org.openjdk.jmh.annotations.Scope;
3133
import org.openjdk.jmh.annotations.Setup;
3234
import org.openjdk.jmh.annotations.State;
35+
import org.openjdk.jmh.annotations.Warmup;
3336
import org.openjdk.jmh.infra.Blackhole;
3437

3538
import java.util.Random;
3639
import java.util.concurrent.TimeUnit;
3740

3841
/**
39-
* Tests java.lang.Integer
42+
* Test various java.lang.Integer operations
4043
*/
4144
@BenchmarkMode(Mode.AverageTime)
4245
@OutputTimeUnit(TimeUnit.MICROSECONDS)
4346
@State(Scope.Thread)
47+
@Warmup(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
48+
@Measurement(iterations = 5, time = 1000, timeUnit = TimeUnit.MILLISECONDS)
49+
@Fork(3)
4450
public class Integers {
4551

4652
@Param("500")
4753
private int size;
4854

4955
private String[] strings;
56+
private int[] intsSmall;
57+
private int[] intsBig;
5058

5159
@Setup
5260
public void setup() {
5361
Random r = new Random(0);
5462
strings = new String[size];
63+
intsSmall = new int[size];
64+
intsBig = new int[size];
5565
for (int i = 0; i < size; i++) {
56-
strings[i] = "" + (r.nextInt(10000) - 5000);
66+
strings[i] = "" + (r.nextInt(10000) - (5000));
67+
intsSmall[i] = 100 * i + i + 103;
68+
intsBig[i] = ((100 * i + i) << 24) + 4543 + i * 4;
5769
}
5870
}
5971

@@ -63,4 +75,20 @@ public void parseInt(Blackhole bh) {
6375
bh.consume(Integer.parseInt(s));
6476
}
6577
}
78+
79+
/** Performs toString on small values, just a couple of digits. */
80+
@Benchmark
81+
public void toStringSmall(Blackhole bh) {
82+
for (int i : intsSmall) {
83+
bh.consume(Integer.toString(i));
84+
}
85+
}
86+
87+
/** Performs toString on large values, roughly 10 digits. */
88+
@Benchmark
89+
public void toStringBig(Blackhole bh) {
90+
for (int i : intsBig) {
91+
bh.consume(Integer.toString(i));
92+
}
93+
}
6694
}

test/micro/org/openjdk/bench/java/lang/Longs.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2014, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -24,20 +24,25 @@
2424

2525
import org.openjdk.jmh.annotations.Benchmark;
2626
import org.openjdk.jmh.annotations.BenchmarkMode;
27+
import org.openjdk.jmh.annotations.Fork;
28+
import org.openjdk.jmh.annotations.Measurement;
2729
import org.openjdk.jmh.annotations.Mode;
2830
import org.openjdk.jmh.annotations.OutputTimeUnit;
2931
import org.openjdk.jmh.annotations.Param;
3032
import org.openjdk.jmh.annotations.Scope;
3133
import org.openjdk.jmh.annotations.Setup;
3234
import org.openjdk.jmh.annotations.State;
33-
import org.openjdk.jmh.annotations.Threads;
35+
import org.openjdk.jmh.annotations.Warmup;
3436
import org.openjdk.jmh.infra.Blackhole;
3537

3638
import java.util.concurrent.TimeUnit;
3739

3840
@BenchmarkMode(Mode.AverageTime)
3941
@OutputTimeUnit(TimeUnit.MICROSECONDS)
4042
@State(Scope.Thread)
43+
@Warmup(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
44+
@Measurement(iterations = 5, time = 1000, timeUnit = TimeUnit.MILLISECONDS)
45+
@Fork(3)
4146
public class Longs {
4247

4348
@Param("500")
@@ -56,18 +61,16 @@ public void setup() {
5661
}
5762
}
5863

59-
/** Performs toString on a bunch of java.lang.Long:s, all with small values, just a couple of digits. */
64+
/** Performs toString on small values, just a couple of digits. */
6065
@Benchmark
61-
@Threads(Threads.MAX)
6266
public void toStringSmall(Blackhole bh) {
6367
for (long value : longArraySmall) {
6468
bh.consume(Long.toString(value));
6569
}
6670
}
6771

68-
/** Performs toString on a bunch of java.lang.Long:s, all with large values, around 10 digits. */
72+
/** Performs toString on large values, around 10 digits. */
6973
@Benchmark
70-
@Threads(Threads.MAX)
7174
public void toStringBig(Blackhole bh) {
7275
for (long value : longArrayBig) {
7376
bh.consume(Long.toString(value));
@@ -80,7 +83,6 @@ public void toStringBig(Blackhole bh) {
8083
public int innerLoops = 1500;
8184

8285
@Benchmark
83-
@Threads(Threads.MAX)
8486
public long repetitiveSubtraction() {
8587
long x = 127, dx = 0;
8688

0 commit comments

Comments
 (0)