csharp
# for i 循环
for (int i = 0; i < list.size(); i++) {
list.get(i);
}
# 增强for循环
for (int item : list) {
}
# iterator for 循环
for (Iterator<Integer> iterator = list.iterator(); iterator.hasNext(); ) {
iterator.next();
}
# iterator while 循环
Iterator<Integer> iterator = list.iterator();
while (iterator.hasNext()) {
iterator.next();
}
# list.forEach 循环
list.forEach(item -> {
});
# list.stream().forEach 循环
list.stream().forEach(item -> {
});
# list.parallelStream().forEach 循环
list.parallelStream().forEach(item -> {
});
性能比较
java
每种循环各遍历10000_0000次 total cost time : 699 ms
for i 循环 : 63 ms, 9.01%
iterator for 循环 : 89 ms, 12.73%
iterator while 循环 : 86 ms, 12.30%
for Boost 循环 : 161 ms, 23.03%
list.forEach 循环 : 115 ms, 16.45%
list.stream().forEach 循环 : 115 ms, 16.45%
list.parallelStream().forEach 循环 : 66 ms, 9.44%