Skip to content

Instantly share code, notes, and snippets.

@dvbobrov
Created December 29, 2013 19:11
Show Gist options
  • Save dvbobrov/8173687 to your computer and use it in GitHub Desktop.
Save dvbobrov/8173687 to your computer and use it in GitHub Desktop.
package com.dbobrov.benchmark;
import org.openjdk.jmh.annotations.*;
import java.util.concurrent.TimeUnit;
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@State
public class ArrayCopy {
private long[] arr;
private int[] iArr;
private Long[] refArr;
@Setup
public void setup() {
arr = new long[100000];
for (int i = 0; i < 100000; i++)
arr[i] = i;
refArr = new Long[100000];
for (int i = 0; i < 100000; i++)
refArr[i] = (long)i;
iArr = new int[100000];
for (int i = 0; i < 100000; i++)
iArr[i] = i;
}
@GenerateMicroBenchmark
public long[] system() {
long[] res = new long[arr.length];
System.arraycopy(arr, 0, res, 0, arr.length);
return res;
}
@GenerateMicroBenchmark
public long[] loop() {
long[] res = new long[arr.length];
for (int i = 0; i < arr.length; i++) {
res[i] = arr[i];
}
return res;
}
@GenerateMicroBenchmark
public int[] systemI() {
int[] res = new int[iArr.length];
System.arraycopy(iArr, 0, res, 0, iArr.length);
return res;
}
@GenerateMicroBenchmark
public int[] loopI() {
int[] res = new int[iArr.length];
for (int i = 0; i < iArr.length; i++) {
res[i] = iArr[i];
}
return res;
}
@GenerateMicroBenchmark
public Long[] systemRef() {
Long[] res = new Long[refArr.length];
System.arraycopy(refArr, 0, res, 0, refArr.length);
return res;
}
@GenerateMicroBenchmark
public Long[] loopRef() {
Long[] res = new Long[refArr.length];
for (int i = 0; i < refArr.length; i++) {
res[i] = refArr[i];
}
return res;
}
}
java -XX:+UseCompressedOops -jar target/microbenchmarks.jar ".*ArrayCopy.*" -i 5 -wi 20 -f 2
Benchmark Mode Thr Count Sec Mean Mean error Units
c.d.b.ArrayCopy.loop avgt 1 10 1 260.713 7.256 us/op
c.d.b.ArrayCopy.loopI avgt 1 10 1 133.645 11.282 us/op
c.d.b.ArrayCopy.loopRef avgt 1 10 1 224.961 18.164 us/op
c.d.b.ArrayCopy.system avgt 1 20 1 179.633 1.203 us/op // runned separately with 40 warmup iterations
c.d.b.ArrayCopy.systemI avgt 1 10 1 91.585 6.170 us/op
c.d.b.ArrayCopy.systemRef avgt 1 10 1 127.822 6.393 us/op
java -XX:-UseCompressedOops -jar target/microbenchmarks.jar ".*ArrayCopy.*" -i 5 -wi 20 -f 2
Benchmark Mode Thr Count Sec Mean Mean error Units
c.d.b.ArrayCopy.loop avgt 1 10 1 262.489 8.358 us/op
c.d.b.ArrayCopy.loopI avgt 1 10 1 129.528 4.491 us/op
c.d.b.ArrayCopy.loopRef avgt 1 10 1 341.100 4.476 us/op
c.d.b.ArrayCopy.system avgt 1 20 1 179.255 2.727 us/op // runned separately with 40 warmup iterations
c.d.b.ArrayCopy.systemI avgt 1 10 1 88.850 1.932 us/op
c.d.b.ArrayCopy.systemRef avgt 1 10 1 256.901 1.876 us/op
# Warmup Iteration 1: 309.028 us/op
# Warmup Iteration 2: 221.333 us/op
# Warmup Iteration 3: 178.869 us/op
# Warmup Iteration 4: 179.689 us/op
# Warmup Iteration 5: 182.037 us/op
# Warmup Iteration 6: 180.258 us/op
# Warmup Iteration 7: 179.259 us/op
# Warmup Iteration 8: 179.932 us/op
# Warmup Iteration 9: 180.366 us/op
# Warmup Iteration 10: 180.112 us/op
# Warmup Iteration 11: 178.558 us/op
# Warmup Iteration 12: 199.839 us/op
# Warmup Iteration 13: 247.690 us/op
# Warmup Iteration 14: 251.402 us/op
# Warmup Iteration 15: 249.377 us/op
# Warmup Iteration 16: 250.163 us/op
# Warmup Iteration 17: 249.931 us/op
# Warmup Iteration 18: 251.042 us/op
# Warmup Iteration 19: 252.276 us/op
# Warmup Iteration 20: 249.913 us/op
# Warmup Iteration 21: 249.253 us/op
# Warmup Iteration 22: 250.548 us/op
# Warmup Iteration 23: 249.159 us/op
# Warmup Iteration 24: 225.688 us/op
# Warmup Iteration 25: 184.037 us/op
# Warmup Iteration 26: 178.373 us/op
# Warmup Iteration 27: 178.506 us/op
# Warmup Iteration 28: 178.471 us/op
# Warmup Iteration 29: 180.717 us/op
# Warmup Iteration 30: 179.385 us/op
# Warmup Iteration 31: 177.574 us/op
# Warmup Iteration 32: 177.429 us/op
# Warmup Iteration 33: 178.025 us/op
# Warmup Iteration 34: 177.862 us/op
# Warmup Iteration 35: 179.531 us/op
# Warmup Iteration 36: 178.716 us/op
# Warmup Iteration 37: 178.471 us/op
# Warmup Iteration 38: 178.945 us/op
# Warmup Iteration 39: 179.210 us/op
# Warmup Iteration 40: 180.171 us/op
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment