Skip to content

Instantly share code, notes, and snippets.

@ejain
Created December 18, 2014 03:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ejain/05253cf8c94b1b874ce6 to your computer and use it in GitHub Desktop.
Save ejain/05253cf8c94b1b874ce6 to your computer and use it in GitHub Desktop.
A JMH microbenchmark comparing sorting performance on Ints.asList vs an actual List.
Benchmark Mode Samples Score Error Units
SortBenchmark.testSortObjectView avgt 7 10.601 ± 4.034 s/op
SortBenchmark.testSortObjects avgt 7 13.497 ± 5.649 s/op
package benchmark;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import com.google.common.collect.Lists;
import com.google.common.primitives.Ints;
@State(Scope.Thread)
public class SortBenchmark {
private final int[] primitives = new int[10_000_000];
@Setup
public void fill() {
Random rand = new Random();
for (int i = 0; i < primitives.length; ++i) {
primitives[i] = rand.nextInt();
}
}
@Benchmark
public int[] testSortObjects() {
List<Integer> values = Lists.newArrayListWithCapacity(primitives.length);
for (int i = 0; i < primitives.length; ++i) {
values.add(primitives[i]);
}
Collections.sort(values);
return Ints.toArray(values);
}
@Benchmark
public int[] testSortObjectView() {
List<Integer> values = Ints.asList(primitives.clone());
Collections.sort(values);
return Ints.toArray(values);
}
public static void main(String... args) throws Exception {
Options opts = new OptionsBuilder()
.include(SortBenchmark.class.getSimpleName())
.mode(Mode.AverageTime)
.warmupIterations(3)
.measurementIterations(7)
.jvmArgs("-server")
.forks(1)
.build();
new Runner(opts).run();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment