forked from YunaiV/JavaLambdaInternals
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StringTest.java
101 lines (93 loc) · 2.48 KB
/
StringTest.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
90
91
92
93
94
95
96
97
98
99
100
101
package lee;
import java.util.ArrayList;
import java.util.Random;
/**
* java -server -Xms10G -Xmx10G -XX:+PrintGCDetails
* -XX:+UseConcMarkSweepGC -XX:CompileThreshold=1000 lee/StringTest
* taskset -c 0-[0,1,3,7] java ...
* @author CarpenterLee
*/
public class StringTest {
public static void main(String[] args) {
new StringTest().doTest();
}
public void doTest(){
warmUp();
int[] lengths = {
10000,
100000,
1000000,
10000000,
20000000,
40000000
};
for(int length : lengths){
System.out.println(String.format("---List length: %d---", length));
ArrayList<String> list = randomStringList(length);
int times = 4;
String min1 = "1";
String min2 = "2";
String min3 = "3";
long startTime;
startTime = System.nanoTime();
for(int i=0; i<times; i++){
min1 = minStringForLoop(list);
}
TimeUtil.outTimeUs(startTime, "minStringForLoop time:", times);
startTime = System.nanoTime();
for(int i=0; i<times; i++){
min2 = minStringStream(list);
}
TimeUtil.outTimeUs(startTime, "minStringStream time:", times);
startTime = System.nanoTime();
for(int i=0; i<times; i++){
min3 = minStringParallelStream(list);
}
TimeUtil.outTimeUs(startTime, "minStringParallelStream time:", times);
System.out.println(min1.equals(min2) && min2.equals(min3));
// System.out.println(min1);
}
}
private void warmUp(){
ArrayList<String> list = randomStringList(10);
for(int i=0; i<20000; i++){
minStringForLoop(list);
minStringStream(list);
minStringParallelStream(list);
}
}
private String minStringForLoop(ArrayList<String> list){
String minStr = null;
boolean first = true;
for(String str : list){
if(first){
first = false;
minStr = str;
}
if(minStr.compareTo(str)>0){
minStr = str;
}
}
return minStr;
}
private String minStringStream(ArrayList<String> list){
return list.stream().min(String::compareTo).get();
}
private String minStringParallelStream(ArrayList<String> list){
return list.stream().parallel().min(String::compareTo).get();
}
private ArrayList<String> randomStringList(int listLength){
ArrayList<String> list = new ArrayList<>(listLength);
Random rand = new Random();
int strLength = 10;
StringBuilder buf = new StringBuilder(strLength);
for(int i=0; i<listLength; i++){
buf.delete(0, buf.length());
for(int j=0; j<strLength; j++){
buf.append((char)('a'+rand.nextInt(26)));
}
list.add(buf.toString());
}
return list;
}
}