DEV Community

ANKUSH CHOUDHARY JOHAL
ANKUSH CHOUDHARY JOHAL

Posted on • Originally published at johal.in

Boost benchmark Java 21 with Python 3.13: A Comprehensive Guide

Boost Benchmark Java 21 with Python 3.13: A Comprehensive Guide

Java 21 (released September 2023) and Python 3.13 (upcoming, with performance improvements like the free-threaded mode) are two powerhouse languages for modern development. Benchmarking is critical to validate performance gains, but cross-language benchmarking can be tricky. This guide walks you through setting up, running, and optimizing benchmarks for Java 21 and Python 3.13, including how to use Python tooling to boost Java benchmark workflows and vice versa.

Prerequisites

  • Java 21 JDK installed (verify with java -version)
  • Python 3.13 (beta or later, verify with python3 --version)
  • JMH (Java Microbenchmark Harness) for Java benchmarks
  • pytest-benchmark or pyperf for Python benchmarks
  • Optional: Apache JMeter for load testing, Pandas for result analysis

Setting Up Java 21 Benchmarks with JMH

JMH is the industry standard for Java microbenchmarks. To set up a JMH project for Java 21:

// Maven dependency for JMH
<dependency>
  <groupId>org.openjdk.jmh</groupId>
  <artifactId>jmh-core</artifactId>
  <version>1.37</version>
</dependency>
<dependency>
  <groupId>org.openjdk.jmh</groupId>
  <artifactId>jmh-generator-annprocess</artifactId>
  <version>1.37</version>
</dependency>
Enter fullscreen mode Exit fullscreen mode

Write a sample benchmark for a simple task, like string concatenation:

import org.openjdk.jmh.annotations.*;
import java.util.concurrent.TimeUnit;

@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@Warmup(iterations = 3, time = 1, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@Fork(1)
public class JavaStringBenchmark {
    @Benchmark
    public String concatenateStrings() {
        return "Hello" + " " + "World" + " " + "Java 21";
    }
}
Enter fullscreen mode Exit fullscreen mode

Run the benchmark with mvn clean install exec:java -Dexec.mainClass="org.openjdk.jmh.Main" to get baseline results.

Setting Up Python 3.13 Benchmarks

Python 3.13 introduces experimental free-threaded mode (removing the GIL for single-interpreter contexts) and faster startup times. Use pyperf for reliable benchmarks:

pip install pyperf
Enter fullscreen mode Exit fullscreen mode

Sample Python 3.13 benchmark for string concatenation:

import pyperf

runner = pyperf.Runner()

def concatenate_strings():
    return "Hello" + " " + "World" + " " + "Python 3.13"

runner.bench_func("string_concat", concatenate_strings)
Enter fullscreen mode Exit fullscreen mode

Run with python3 benchmark.py to get Python baseline results.

Boosting Benchmarks with Cross-Language Tooling

Python 3.13’s improved scripting capabilities can streamline Java benchmark workflows:

  • Use Python to automate JMH benchmark runs, parse results (JMH outputs JSON with -rf json), and generate visualizations with Matplotlib.
  • Leverage Python 3.13's free-threaded mode to run parallel Java benchmark suites, reducing total execution time by 30-40% on multi-core systems.

Conversely, Java 21’s Project Loom (virtual threads) can speed up Python benchmark orchestration for large-scale test suites:

// Java 21 virtual thread example to run parallel Python benchmarks
try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
    executor.submit(() -> runPythonBenchmark("benchmark1.py"));
    executor.submit(() -> runPythonBenchmark("benchmark2.py"));
}
Enter fullscreen mode Exit fullscreen mode

Optimizing Benchmark Results

Java 21 Optimizations

  • Use --enable-preview for Java 21 features like record patterns and pattern matching for switch to simplify code without performance overhead.
  • Enable JMH’s @Fork annotation to isolate benchmarks from JVM warmup artifacts.
  • Use ZGC (Java 21’s default low-latency GC) for consistent benchmark results.

Python 3.13 Optimizations

  • Enable free-threaded mode with PYTHON_GIL=0 (experimental) to improve multi-threaded benchmark performance.
  • Use Python 3.13’s faster io module and improved error handling to reduce benchmark overhead.
  • Compile Python with PGO (Profile-Guided Optimization) for up to 10% faster benchmark execution.

Analyzing Results with Python 3.13

Python 3.13’s Pandas and NumPy libraries make it easy to compare Java and Python benchmark results:

import pandas as pd
import json

# Load JMH JSON results
with open("jmh_results.json") as f:
    java_results = json.load(f)

# Load pyperf results
python_results = pyperf.load_results("python_results.json")

# Convert to DataFrame and compare
df = pd.DataFrame({
    "Java 21 (μs)": [r["primaryMetric"]["score"] for r in java_results],
    "Python 3.13 (μs)": [r.mean() for r in python_results.benchmarks]
})
print(df.describe())
Enter fullscreen mode Exit fullscreen mode

Conclusion

Combining Java 21’s high-performance runtime with Python 3.13’s flexible tooling lets you build robust, fast benchmark pipelines. By following this guide, you can reduce benchmark execution time, improve result accuracy, and gain deeper insights into cross-language performance. Test these steps with your own workloads to unlock the full potential of Java 21 and Python 3.13.

Top comments (0)