0

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?

8
  • 2
    Imports are a compile-time construct. They don't involve any runtime overhead other than the usual classloading.
    – shmosel
    Commented Feb 28 at 1:37
  • What library are you expecting to measure the overhead of importing? JMH? IntStream? In any event, any sane benchmark measures only after everything is fully loaded in, since all JVM code runs slowly as it starts up even after it's initially loaded. Commented Feb 28 at 2:58
  • @LouisWasserman Yes, I wish to know the overhead for the java.util libraries like IntStream and Collectors (if any).
    – Ymi
    Commented Feb 28 at 3:03
  • What kind of overhead are you talking about? Imports are, as previously stated, a compile-time construct. Loading a class takes time but will only incur a cost the first time the class is used. Using an 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 to shuffle 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.
    – Slaw
    Commented Feb 28 at 4:20
  • 1
    Then your benchmark method seems fine, other than the potential issue described in my second comment. You don't have to worry about "importing" the classes, since 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).
    – Slaw
    Commented Feb 28 at 7:27

0

Browse other questions tagged or ask your own question.