Skip to content
Permalink
Browse files
8271840: Add simple Integer.toString microbenchmarks
Reviewed-by: shade
  • Loading branch information
cl4es committed Aug 5, 2021
1 parent 18dd4d4 commit 55bd52a14283033d66cd7bf1deadb31c040b09c7
Showing 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.
*
* This code is free software; you can redistribute it and/or modify it
@@ -24,36 +24,48 @@

import org.openjdk.jmh.annotations.Benchmark;
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.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.infra.Blackhole;

import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
* Tests java.lang.Integer
* Test various java.lang.Integer operations
*/
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@State(Scope.Thread)
@Warmup(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
@Measurement(iterations = 5, time = 1000, timeUnit = TimeUnit.MILLISECONDS)
@Fork(3)
public class Integers {

@Param("500")
private int size;

private String[] strings;
private int[] intsSmall;
private int[] intsBig;

@Setup
public void setup() {
Random r = new Random(0);
strings = new String[size];
intsSmall = new int[size];
intsBig = new int[size];
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 void parseInt(Blackhole bh) {
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.
*
* This code is free software; you can redistribute it and/or modify it
@@ -24,20 +24,25 @@

import org.openjdk.jmh.annotations.Benchmark;
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.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
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 java.util.concurrent.TimeUnit;

@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@State(Scope.Thread)
@Warmup(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
@Measurement(iterations = 5, time = 1000, timeUnit = TimeUnit.MILLISECONDS)
@Fork(3)
public class Longs {

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

/** 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
@Threads(Threads.MAX)
public void toStringSmall(Blackhole bh) {
for (long value : longArraySmall) {
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
@Threads(Threads.MAX)
public void toStringBig(Blackhole bh) {
for (long value : longArrayBig) {
bh.consume(Long.toString(value));
@@ -80,7 +83,6 @@ public void toStringBig(Blackhole bh) {
public int innerLoops = 1500;

@Benchmark
@Threads(Threads.MAX)
public long repetitiveSubtraction() {
long x = 127, dx = 0;

1 comment on commit 55bd52a

@openjdk-notifier
Copy link

@openjdk-notifier openjdk-notifier bot commented on 55bd52a Aug 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.