Created
November 15, 2014 20:41
-
-
Save twillouer/ac13eb1dadc8a270f821 to your computer and use it in GitHub Desktop.
Benchmarking of toArray
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@State(Scope.Benchmark) | |
public class ToArrayBench { | |
ArrayList<Byte> list; | |
@Setup | |
public void setup() throws Throwable | |
{ | |
list = new ArrayList<>(); | |
for (int i = 0; i < 10; i++) { | |
list.add((byte) i); | |
} | |
} | |
@Benchmark | |
public void zero_sized_array() | |
{ | |
list.toArray(new Byte[0]); | |
} | |
@Benchmark | |
public void simple_toArray() | |
{ | |
list.toArray(); | |
} | |
@Benchmark | |
public void sized_array_from_list() | |
{ | |
list.toArray(new Byte[list.size()]); | |
} | |
@Benchmark | |
public void sized_array_fixed_size() | |
{ | |
list.toArray(new Byte[100]); | |
} | |
@Benchmark | |
public void defensive_copy() | |
{ | |
new ArrayList<>(list); | |
} | |
public static void main(String[] args) throws RunnerException, IOException | |
{ | |
Options opt = new OptionsBuilder().include(".*" + ToArrayBench.class.getSimpleName() + ".*") | |
.warmupIterations(20) | |
.warmupTime(TimeValue.seconds(1)) | |
.measurementIterations(20) | |
.timeUnit(TimeUnit.MILLISECONDS) | |
.forks(1) | |
// .addProfiler(LinuxPerfProfiler.class) | |
.build(); | |
new Runner(opt).run(); | |
} | |
} |
@jerrinot thanks for your time.
Still in trouble to understand the problem :)
No idea either, can someone please elaborate on that ?
Updated:
@State(Scope.Benchmark)
public class ToArrayBench {
private static final int SIZE = 100;
ArrayList<Byte> list;
@Setup
public void setup() throws Throwable
{
list = new ArrayList<>();
for (int i = 0; i < SIZE; i++) {
list.add((byte) i);
}
}
@Benchmark
public void zero_sized_array(Blackhole bh)
{
bh.consume(list.toArray(new Byte[0]));
}
@Benchmark
public void simple_toArray(Blackhole bh)
{
bh.consume(list.toArray());
}
@Benchmark
public void sized_array_from_list(Blackhole bh)
{
bh.consume(list.toArray(new Byte[list.size()]));
}
@Benchmark
public void sized_array_fixed_size(Blackhole bh)
{
bh.consume(list.toArray(new Byte[SIZE]));
}
@Benchmark
public void defensive_copy(Blackhole bh)
{
bh.consume(new ArrayList<>(list));
}
public static void main(String[] args) throws RunnerException, IOException
{
Options opt = new OptionsBuilder().include(".*" + ToArrayBench.class.getSimpleName() + ".*")
.warmupIterations(20)
.warmupTime(TimeValue.seconds(1))
.measurementIterations(20)
.timeUnit(TimeUnit.MILLISECONDS)
.forks(1)
// .addProfiler(LinuxPerfAsmProfiler.class)
.build();
new Runner(opt).run();
}
}
Benchmark | Mode | Cnt | Score | Error | Units |
---|---|---|---|---|---|
ToArrayBench.defensive_copy | thrpt | 200 | 16 714 192 | ± 129515,217 | ops/s |
ToArrayBench.simple_toArray | thrpt | 200 | 17 918 950 | ± 102801,298 | ops/s |
ToArrayBench.sized_array_fixed_size | thrpt | 200 | 5 799 136 | ± 65921,564 | ops/s |
ToArrayBench.sized_array_from_list | thrpt | 200 | 5 643 162 | ± 85215,009 | ops/s |
ToArrayBench.zero_sized_array | thrpt | 200 | 6 529 068 | ± 78960,062 | ops/s |
Routinely, I will chew on people who can't use perfasm
profiler, but this is not your fault it wasn't helping here. ;) Only in JMH 1.5+ (released yesterday) perfasm can decode the VM stubs, and VM stubs are the crucial piece of info to untangle this. See: http://cr.openjdk.java.net/~shade/scratch/ToArrayBench.java
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this is giving me similarly confusing results:
perf stats for fast():
perf stats for slow():