Java 测试迭代集合耗时
Java About 8,979 words迭代 List 耗时
@Test
public void testIterateList() {
List<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
list.add("e");
list.add("f");
list.add("g");
list.add("h");
list.add("i");
list.add("j");
long time0 = System.nanoTime();
for (String s : list) {
String s1 = s;
}
long time1 = System.nanoTime();
System.out.println("增强for循环耗时: " + (time1 - time0));
long time2 = System.nanoTime();
for (int i = 0; i < list.size(); i++) {
String s1 = list.get(i);
}
long time3 = System.nanoTime();
System.out.println("fori循环耗时: " + (time3 - time2));
long time4 = System.nanoTime();
list.forEach(l -> {
String s1 = l;
});
long time5 = System.nanoTime();
System.out.println("Java8 forEach循环耗时: " + (time5 - time4));
Collection<String> c = list;
long time6 = System.nanoTime();
Iterator<String> it = c.iterator();
while(it.hasNext()){
String ele = it.next();
}
long time7 = System.nanoTime();
System.out.println("Iterator迭代耗时: " + (time7 - time6));
}
List 耗时:nonoTime
- fori循环耗时: 8754
- Iterator迭代耗时: 12376
- 增强for循环耗时: 17207
- Java8 forEach循环耗时: 67216082
迭代 Set 耗时
@Test
public void testIterateSet() {
Set<String> set = new HashSet<>();
set.add("a");
set.add("b");
set.add("c");
set.add("d");
set.add("e");
set.add("f");
set.add("g");
set.add("h");
set.add("i");
set.add("j");
long time0 = System.nanoTime();
for (String s : set) {
String s1 = s;
}
long time1 = System.nanoTime();
System.out.println("增强for循环耗时: " + (time1 - time0));
long time2 = System.nanoTime();
Iterator<String> iterator = set.iterator();
while (iterator.hasNext()) {
String s1 = iterator.next();
}
long time3 = System.nanoTime();
System.out.println("Iterator迭代耗时: " + (time3 - time2));
long time4 = System.nanoTime();
set.forEach(s -> {
String s1 = s;
});
long time5 = System.nanoTime();
System.out.println("Java8 forEach循环耗时: " + (time5 - time4));
}
Set 耗时:nonoTime
- Iterator迭代耗时: 3018
- 增强for循环耗时: 76673
- Java8 forEach循环耗时: 37936942
迭代 Map 耗时
@Test
public void testIterateMap() {
Map<String,Integer> map = new HashMap<>();
map.put("a",10);
map.put("b",20);
map.put("c",30);
map.put("d",40);
map.put("e",50);
map.put("f",60);
map.put("g",70);
map.put("h",80);
map.put("i",90);
map.put("j",100);
long time0 = System.nanoTime();
for (Map.Entry<String, Integer> entry : map.entrySet()) {
String key = entry.getKey();
Integer value = entry.getValue();
}
long time1 = System.nanoTime();
System.out.println("增强for循环耗时: " + (time1 - time0));
long time2 = System.nanoTime();
Set<Map.Entry<String, Integer>> entries = map.entrySet();
for (int i = 0; i < entries.size(); i++) {
Iterator<Map.Entry<String, Integer>> it = entries.iterator();
while (it.hasNext()) {
Map.Entry<String, Integer> entry = it.next();
String key = entry.getKey();
Integer value = entry.getValue();
}
}
long time3 = System.nanoTime();
System.out.println("fori循环耗时: " + (time3 - time2));
long time4 = System.nanoTime();
map.forEach((k,v) ->{
String k1 = k;
Integer v1 = v;
});
long time5 = System.nanoTime();
System.out.println("Java8 forEach循环耗时: " + (time5 - time4));
long time6 = System.nanoTime();
Iterator<Map.Entry<String, Integer>> iterator = map.entrySet().iterator();
while (iterator.hasNext()){
Map.Entry<String,Integer> entry = iterator.next();
String key = entry.getKey();
Integer value = entry.getValue();
}
long time7 = System.nanoTime();
System.out.println("Iterator迭代耗时: " + (time7 - time6));
long time8 = System.nanoTime();
for (String key : map.keySet()){
Integer value = map.get(key);
}
long time9 = System.nanoTime();
System.out.println("keySet迭代耗时: " + (time9 - time8));
}
Map 耗时: nonoTime
- Iterator迭代耗时: 9056
- 增强for循环耗时: 44072
- fori循环耗时: 84522
- keySet迭代耗时: 123463
- Java8 forEach循环耗时: 57128945
测试类
public class TestIterateElapsedTime {
@Test
public void testIterateList() {
List<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
list.add("e");
list.add("f");
list.add("g");
list.add("h");
list.add("i");
list.add("j");
long time0 = System.nanoTime();
for (String s : list) {
String s1 = s;
}
long time1 = System.nanoTime();
System.out.println("增强for循环耗时: " + (time1 - time0));
long time2 = System.nanoTime();
for (int i = 0; i < list.size(); i++) {
String s1 = list.get(i);
}
long time3 = System.nanoTime();
System.out.println("fori循环耗时: " + (time3 - time2));
long time4 = System.nanoTime();
list.forEach(l -> {
String s1 = l;
});
long time5 = System.nanoTime();
System.out.println("Java8 forEach循环耗时: " + (time5 - time4));
Collection<String> c = list;
long time6 = System.nanoTime();
Iterator<String> it = c.iterator();
while(it.hasNext()){
String ele = it.next();
}
long time7 = System.nanoTime();
System.out.println("Iterator迭代耗时: " + (time7 - time6));
}
@Test
public void testIterateSet() {
Set<String> set = new HashSet<>();
set.add("a");
set.add("b");
set.add("c");
set.add("d");
set.add("e");
set.add("f");
set.add("g");
set.add("h");
set.add("i");
set.add("j");
long time0 = System.nanoTime();
for (String s : set) {
String s1 = s;
}
long time1 = System.nanoTime();
System.out.println("增强for循环耗时: " + (time1 - time0));
long time2 = System.nanoTime();
Iterator<String> iterator = set.iterator();
while (iterator.hasNext()) {
String s1 = iterator.next();
}
long time3 = System.nanoTime();
System.out.println("Iterator迭代耗时: " + (time3 - time2));
long time4 = System.nanoTime();
set.forEach(s -> {
String s1 = s;
});
long time5 = System.nanoTime();
System.out.println("Java8 forEach循环耗时: " + (time5 - time4));
}
@Test
public void testIterateMap() {
Map<String,Integer> map = new HashMap<>();
map.put("a",10);
map.put("b",20);
map.put("c",30);
map.put("d",40);
map.put("e",50);
map.put("f",60);
map.put("g",70);
map.put("h",80);
map.put("i",90);
map.put("j",100);
long time0 = System.nanoTime();
for (Map.Entry<String, Integer> entry : map.entrySet()) {
String key = entry.getKey();
Integer value = entry.getValue();
}
long time1 = System.nanoTime();
System.out.println("增强for循环耗时: " + (time1 - time0));
long time2 = System.nanoTime();
Set<Map.Entry<String, Integer>> entries = map.entrySet();
for (int i = 0; i < entries.size(); i++) {
Iterator<Map.Entry<String, Integer>> it = entries.iterator();
while (it.hasNext()) {
Map.Entry<String, Integer> entry = it.next();
String key = entry.getKey();
Integer value = entry.getValue();
}
}
long time3 = System.nanoTime();
System.out.println("fori循环耗时: " + (time3 - time2));
long time4 = System.nanoTime();
map.forEach((k,v) ->{
String k1 = k;
Integer v1 = v;
});
long time5 = System.nanoTime();
System.out.println("Java8 forEach循环耗时: " + (time5 - time4));
long time6 = System.nanoTime();
Iterator<Map.Entry<String, Integer>> iterator = map.entrySet().iterator();
while (iterator.hasNext()){
Map.Entry<String,Integer> entry = iterator.next();
String key = entry.getKey();
Integer value = entry.getValue();
}
long time7 = System.nanoTime();
System.out.println("Iterator迭代耗时: " + (time7 - time6));
long time8 = System.nanoTime();
for (String key : map.keySet()){
Integer value = map.get(key);
}
long time9 = System.nanoTime();
System.out.println("keySet迭代耗时: " + (time9 - time8));
}
}
Views: 3,721 · Posted: 2019-04-08
————        END        ————
Give me a Star, Thanks:)
https://github.com/fendoudebb/LiteNote扫描下方二维码关注公众号和小程序↓↓↓
Loading...