遍历效率测试:
public class TestCollection {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
for (int i = 0; i < 3000 * 10000; i++) {
list.add("list - " + i);
}
long time = System.currentTimeMillis();
list.forEach(new Consumer<String>() {
@Override
public void accept(String str) {
stringF(str);
}
});
System.out.println("forEach *** " + (System.currentTimeMillis() - time));
time = System.currentTimeMillis();
for (String str : list) {
stringF(str);
}
System.out.println("for : *** " + (System.currentTimeMillis() - time));
time = System.currentTimeMillis();
for (int i = 0; i < list.size(); i++) {
stringF(list.get(i));
}
System.out.println("for size *** " + (System.currentTimeMillis() - time));
time = System.currentTimeMillis();
for (Iterator<String> it = list.iterator(); it.hasNext(); ) {
stringF(it.next());
}
System.out.println("Iterator *** " + (System.currentTimeMillis() - time));
}
private static void stringF(String str) {
if (str != null && str.trim().length() > 3) {
String temp = str.substring(0, 2);
if (temp.contains("s")) {
temp = temp + "666";
} else if (temp.contains("A")) {
temp = temp + "555";
} else if (temp.contains("b")) {
temp = temp + "444";
} else if (temp.contains("c")) {
temp = temp + "333";
} else if (temp.contains("d")) {
temp = temp + "222";
} else if (temp.contains("1")) {
temp = temp + "555";
} else if (temp.contains("2")) {
temp = temp + "444";
} else if (temp.contains("3")) {
temp = temp + "333";
} else if (temp.contains("4")) {
temp = temp + "222";
} else if (temp.contains("5")) {
temp = temp + "555";
} else if (temp.contains("7")) {
temp = temp + "444";
} else if (temp.contains("8")) {
temp = temp + "333";
} else if (temp.contains("9")) {
temp = temp + "222";
} else if (temp.contains("0")) {
temp = temp + "555";
} else if (temp.contains("q")) {
temp = temp + "444";
} else if (temp.contains("w")) {
temp = temp + "333";
} else if (temp.contains("e")) {
temp = temp + "222";
} else if (temp.contains("r")) {
temp = temp + "555";
} else if (temp.contains("t")) {
temp = temp + "444";
} else if (temp.contains("y")) {
temp = temp + "333";
} else if (temp.contains("u")) {
temp = temp + "222";
}
}
}
}
1. size=3000*10000, stringF() 方法不为空(执行如上 if-else)
forEach *** 3773
for : *** 2466
for size *** 2468
Iterator *** 2482
2. size=1000*10000, stringF() 方法不为空(执行如上 if-else)
forEach *** 1042
for : *** 956
for size *** 914
Iterator *** 923
3. size=3000*10000, stringF() 为空方法
forEach *** 142
for : *** 140
for size *** 143
Iterator *** 137
4. size=300*10000, stringF() 方法不为空(执行如上 if-else)
forEach *** 251
for : *** 286
for size *** 233
Iterator *** 244
5. size=300*10000, stringF() 为空方法
forEach *** 17
for : *** 18
for size *** 16
Iterator *** 17
6. size=3*10000, stringF() 方法不为空(执行如上 if-else)
forEach *** 20
for : *** 4
for size *** 4
Iterator *** 4
6. size=0.3*10000, stringF() 方法不为空(执行如上 if-else)
forEach *** 5
for : *** 2
for size *** 2
Iterator *** 1
7. size=0.03*10000, stringF() 方法不为空(执行如上 if-else)
forEach *** 4
for : *** 0
for size *** 1
Iterator *** 0
网友评论