/
ArrayCopyBenchmarks.java
64 lines (54 loc) · 1.73 KB
/
ArrayCopyBenchmarks.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.patotski.performance.array;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Random;
import static com.patotski.performance.utils.BenchmarkUtils.runBenchmark;
public class ArrayCopyBenchmarks {
@State(Scope.Benchmark)
public static class ArrState {
public int[] arr = new int[3000];
public int[] arr2 = new int[3000];
@Setup
public void setup() {
Random random = new Random();
Arrays.fill(arr, random.nextInt());
}
}
//@Benchmark
public int[] loopCopy(ArrState state) {
int[] arr2 = new int[30];
for (int i = 0; i < state.arr.length; i++) {
arr2[i] = state.arr[i];
}
return arr2;
}
//@Benchmark
public int[] systemArraysCopy(ArrState state) {
int[] arr2 = new int[30];
System.arraycopy(state.arr, 0, arr2, 0, state.arr.length);
return arr2;
}
@Benchmark
public int[] systemArraysCopyToExistingArray(ArrState state) {
System.arraycopy(state.arr, 0, state.arr2, 0, state.arr.length);
return state.arr2;
}
@Benchmark
public int[] loopCopyToExistingArray(ArrState state) {
for (int i = 0; i < state.arr.length; i++) {
state.arr2[i] = state.arr[i];
}
return state.arr2;
}
//@Benchmark
public int[] arraysCopy(ArrState state) {
return Arrays.copyOf(state.arr, state.arr.length);
}
public static void main(String[] args) throws Exception {
runBenchmark(ArrayCopyBenchmarks.class);
}
}