forked from CarpenterLee/JavaLambdaInternals
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IntTest.java
89 lines (80 loc) · 1.93 KB
/
IntTest.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package lee;
import java.util.Arrays;
import java.util.Random;
/**
* java -server -Xms10G -Xmx10G -XX:+PrintGCDetails
* -XX:+UseConcMarkSweepGC -XX:CompileThreshold=1000 lee/IntTest
* taskset -c 0-[0,1,3,7] java ...
* @author CarpenterLee
*/
public class IntTest {
public static void main(String[] args) {
new IntTest().doTest();
}
public void doTest(){
warmUp();
int[] lengths = {
10000,
100000,
1000000,
10000000,
100000000,
1000000000
};
for(int length : lengths){
System.out.println(String.format("---array length: %d---", length));
int[] arr = new int[length];
randomInt(arr);
int times = 4;
int min1 = 1;
int min2 = 2;
int min3 = 3;
long startTime;
startTime = System.nanoTime();
for(int i=0; i<times; i++){
min1 = minIntFor(arr);
}
TimeUtil.outTimeUs(startTime, "minIntFor time:", times);
startTime = System.nanoTime();
for(int i=0; i<times; i++){
min2 = minIntStream(arr);
}
TimeUtil.outTimeUs(startTime, "minIntStream time:", times);
startTime = System.nanoTime();
for(int i=0; i<times; i++){
min3 = minIntParallelStream(arr);
}
TimeUtil.outTimeUs(startTime, "minIntParallelStream time:", times);
System.out.println(min1==min2 && min2==min3);
}
}
private void warmUp(){
int[] arr = new int[100];
randomInt(arr);
for(int i=0; i<20000; i++){
// minIntFor(arr);
minIntStream(arr);
minIntParallelStream(arr);
}
}
private int minIntFor(int[] arr){
int min = Integer.MAX_VALUE;
for(int i=0; i<arr.length; i++){
if(arr[i]<min)
min = arr[i];
}
return min;
}
private int minIntStream(int[] arr){
return Arrays.stream(arr).min().getAsInt();
}
private int minIntParallelStream(int[] arr){
return Arrays.stream(arr).parallel().min().getAsInt();
}
private void randomInt(int[] arr){
Random r = new Random();
for(int i=0; i<arr.length; i++){
arr[i] = r.nextInt();
}
}
}