Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# JMH version: 1.19
# VM version: JDK 1.8.0_101, VM 25.101-b13
# VM invoker: C:\Program Files\Java\jdk1.8.0_101\jre\bin\java.exe
# VM options: <none>
# Run complete. Total time: 00:18:42
Benchmark (collectionType) (size) Mode Cnt Score Error Units
ToArrayTest.normalToArray ArrayList 0 avgt 30 3,731 ± 0,020 ns/op
ToArrayTest.normalToArray ArrayList 1 avgt 30 12,402 ± 0,082 ns/op
ToArrayTest.normalToArray ArrayList 5 avgt 30 15,737 ± 0,120 ns/op
ToArrayTest.normalToArray ArrayList 10 avgt 30 21,685 ± 0,147 ns/op
ToArrayTest.normalToArray ArrayList 20 avgt 30 35,245 ± 0,378 ns/op
ToArrayTest.normalToArray ArrayList 30 avgt 30 49,381 ± 0,811 ns/op
ToArrayTest.normalToArray HashSet 0 avgt 30 4,330 ± 0,025 ns/op
ToArrayTest.normalToArray HashSet 1 avgt 30 31,083 ± 0,238 ns/op
ToArrayTest.normalToArray HashSet 5 avgt 30 44,210 ± 0,288 ns/op
ToArrayTest.normalToArray HashSet 10 avgt 30 62,428 ± 1,107 ns/op
ToArrayTest.normalToArray HashSet 20 avgt 30 83,525 ± 1,206 ns/op
ToArrayTest.normalToArray HashSet 30 avgt 30 126,942 ± 2,163 ns/op
ToArrayTest.normalToArray TreeSet 0 avgt 30 4,734 ± 0,095 ns/op
ToArrayTest.normalToArray TreeSet 1 avgt 30 26,284 ± 0,160 ns/op
ToArrayTest.normalToArray TreeSet 5 avgt 30 39,548 ± 0,293 ns/op
ToArrayTest.normalToArray TreeSet 10 avgt 30 57,699 ± 0,364 ns/op
ToArrayTest.normalToArray TreeSet 20 avgt 30 97,943 ± 1,125 ns/op
ToArrayTest.normalToArray TreeSet 30 avgt 30 133,310 ± 2,507 ns/op
ToArrayTest.oldToArray ArrayList 0 avgt 30 3,429 ± 0,116 ns/op
ToArrayTest.oldToArray ArrayList 1 avgt 30 25,638 ± 0,841 ns/op
ToArrayTest.oldToArray ArrayList 5 avgt 30 32,813 ± 1,607 ns/op
ToArrayTest.oldToArray ArrayList 10 avgt 30 39,479 ± 1,449 ns/op
ToArrayTest.oldToArray ArrayList 20 avgt 30 47,414 ± 0,641 ns/op
ToArrayTest.oldToArray ArrayList 30 avgt 30 60,191 ± 0,411 ns/op
ToArrayTest.oldToArray HashSet 0 avgt 30 3,247 ± 0,080 ns/op
ToArrayTest.oldToArray HashSet 1 avgt 30 31,977 ± 0,511 ns/op
ToArrayTest.oldToArray HashSet 5 avgt 30 45,557 ± 0,515 ns/op
ToArrayTest.oldToArray HashSet 10 avgt 30 59,399 ± 0,394 ns/op
ToArrayTest.oldToArray HashSet 20 avgt 30 81,507 ± 0,808 ns/op
ToArrayTest.oldToArray HashSet 30 avgt 30 123,301 ± 0,611 ns/op
ToArrayTest.oldToArray TreeSet 0 avgt 30 3,638 ± 0,117 ns/op
ToArrayTest.oldToArray TreeSet 1 avgt 30 24,148 ± 0,179 ns/op
ToArrayTest.oldToArray TreeSet 5 avgt 30 41,170 ± 0,401 ns/op
ToArrayTest.oldToArray TreeSet 10 avgt 30 59,199 ± 0,643 ns/op
ToArrayTest.oldToArray TreeSet 20 avgt 30 96,660 ± 0,716 ns/op
ToArrayTest.oldToArray TreeSet 30 avgt 30 131,524 ± 1,082 ns/op
package com.example;
import org.openjdk.jmh.annotations.*;
import java.util.*;
import java.util.concurrent.*;import java.util.function.Supplier;import java.util.stream.Collectors;import java.util.stream.IntStream;
@Fork(3)
@Warmup(iterations=10, time=500, timeUnit=TimeUnit.MILLISECONDS)
@Measurement(iterations=10, time=500, timeUnit=TimeUnit.MILLISECONDS)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Benchmark)
public class ToArrayTest {
private static final int ARRAY_COPY_THRESHOLD = 20;
public static final String[] EMPTY_STRING_ARRAY = new String[0];
@Param({"0", "1", "5", "10", "20", "30"})
private int size;
@Param({"ArrayList", "HashSet", "TreeSet"})
private String collectionType;
private Collection<String> c;
@Setup
public void setup() throws Exception {
Class<? extends Collection> aClass = Class.forName("java.util."+collectionType).asSubclass(Collection.class);
Supplier<Collection<String>> supplier = () -> {
try {
//noinspection unchecked
return aClass.newInstance();
}
catch (InstantiationException | IllegalAccessException e) {
throw new RuntimeException(e);
}
};
c = IntStream.range(0, size).mapToObj(String::valueOf).collect(Collectors.toCollection(supplier));
}
public static <T> T[] toArray(Collection<T> c, T[] sample) {
final int size = c.size();
if (size == sample.length && size < ARRAY_COPY_THRESHOLD) {
int i = 0;
for (T t : c) {
sample[i++] = t;
}
return sample;
}
return c.toArray(sample);
}
public static String[] toStringArray(Collection<String> collection) {
return collection == null || collection.isEmpty()
? EMPTY_STRING_ARRAY : toArray(collection, new String[collection.size()]);
}
@Benchmark
public String[] oldToArray() {
return toStringArray(c);
}
@Benchmark
public String[] normalToArray() {
return c.toArray(EMPTY_STRING_ARRAY);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.