I have the following code that I am benchmarking using JMH
package org.example;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Mode;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class BenchmarkRunner {
@Benchmark
@BenchmarkMode(Mode.AverageTime)
public void bench_Rand() {
List<Integer> pages = IntStream.range(0, 256).boxed().collect(Collectors.toList());
Collections.shuffle(pages);
}
public static void main(String[] args) throws Exception {
org.openjdk.jmh.Main.main(args);
}
}
While the benchmark works, I am wondering about the latency values incurred for importing the libraries in the benchmark code. Is there a way to benchmark the library import together with the code?
IntStream
to iterate a range of numbers will likely have more overhead than a traditional for loop. So, the question becomes, what are you trying to benchmark? If it's the call toshuffle
and you don't want the generation of the list to be counted in the average time, then don't generate the list in the benchmark method. Pass the list in as state.import
statements only have meaning at compile-time. And you don't have to worry about the overhead of class loading, as loading the class will only happen once (and will happen before measurements are taken anyway--during the warmup phase at the latest).