Backend Development 25 min read

Microbenchmarking Integer-to-String Conversions in Java Using JMH

This article examines the performance of four Java integer-to-string conversion methods—Integer.toString, String.valueOf, a+"", and ""+a—by conducting raw loop tests and detailed JMH microbenchmarks, analyzing JVM warm-up, JIT, and OSR effects to reveal reliable measurement practices.

JD Retail Technology
JD Retail Technology
JD Retail Technology
Microbenchmarking Integer-to-String Conversions in Java Using JMH

Major internet companies now emphasize code quality; this article explores microbenchmark performance testing of four common ways to convert an int to a String in Java: Integer.toString(a) , String.valueOf(a) , a + "" , and "" + a .

Initial naive loop tests on a MacBook i7 with 1,000,000 iterations show a+"" slower than ""+a , and String.valueOf(a) slightly slower than Integer.toString(a) . With 500,000,000 iterations the gap narrows.

Further experiments changing loop order reveal that results can reverse, highlighting the impact of JVM warm‑up, JIT optimizations such as OSR, loop unrolling, and method inlining.

Javap inspection shows that a+"" and ""+a both compile to StringBuilder appends, while constant concatenations are folded at compile time.

Disabling OSR (‑XX:-UseOnStackReplacement) and adding a warm‑up loop eliminates the order‑dependent discrepancy, confirming that hotspot compilation influences timings.

The article then introduces JMH (Java Microbenchmark Harness) as a reliable tool for such measurements, providing a full JMH benchmark class with @State, @Warmup, @Measurement, @Fork, @BenchmarkMode, and @OutputTimeUnit annotations, and shows sample benchmark results where the average times for the four methods are reported in nanoseconds.

Key take‑aways: use JMH instead of ad‑hoc loops, avoid dead‑code elimination, be aware of constant folding, and control JVM optimizations to obtain trustworthy microbenchmark data.

public class CommonTest {
    public static void main(String[] args) {
        int num = 1000000;
        int a = 123456789;
        long start = System.currentTimeMillis();
        for (int i = 0; i < num; i++) {
            String m = a + "";
        }
        long end = System.currentTimeMillis();
        System.out.println("a+\"\" = " + (end - start));
        // ... similar loops for ""+a, String.valueOf(a), Integer.toString(a)
    }
}
@State(Scope.Thread)
@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@Fork(1)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public class JMHTest {
    private int a = 123456;

    @Benchmark
    public String work1() { return "" + a; }

    @Benchmark
    public String work2() { return a + ""; }

    @Benchmark
    public String work3() { return Integer.toString(a); }

    @Benchmark
    public String work4() { return String.valueOf(a); }

    public static void main(String[] args) throws RunnerException {
        Options opt = new OptionsBuilder()
            .include(JMHTest.class.getSimpleName())
            .forks(1)
            .build();
        new Runner(opt).run();
    }
}
# JMH version: 1.21
# VM version: JDK 1.8.0_291, Java HotSpot(TM) 64-Bit Server VM, 25.291-b10
# Benchmark mode: Average time, time/op
# Benchmark: JMHTest.work1
Iteration 1: 15.982 ns/op
Iteration 2: 15.901 ns/op
... (similar output for work2, work3, work4)
Benchmark      Mode  Cnt   Score   Error  Units
JMHTest.work1 avgt    5  15.936 ± 0.219 ns/op
JMHTest.work2 avgt    5  15.885 ± 0.149 ns/op
JMHTest.work3 avgt    5  25.779 ± 0.327 ns/op
JMHTest.work4 avgt    5  27.547 ± 7.439 ns/op
JavaJITMicrobenchmarkJMHPerformanceTesting
JD Retail Technology
Written by

JD Retail Technology

Official platform of JD Retail Technology, delivering insightful R&D news and a deep look into the lives and work of technologists.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.