我是靠谱客的博主 柔弱冬日,这篇文章主要介绍php Foreach 和 迭代器,Iterator迭代器和foreach增强for循环的效率比较,现在分享给大家,希望可以做个参考。

package cn.jiguang.base64;

import java.util.HashMap;

import java.util.Iterator;

import java.util.Map;

import java.util.Map.Entry;

import org.junit.Test;

public class CollectionTest {

public void JDK8DataTimeClockTest() {

}

@Test

public void foreachQuery() {

int count = 0;

Map map = new HashMap();

for (int i = 0; i < 100000; i++) {

map.put("key=" + i, "value=" + i);

}

long startTimeFor = System.currentTimeMillis();

for (Entry entry : map.entrySet()) {

System.out.println(entry.getKey());

System.out.println(entry.getValue());

count++;

}

System.out.println("查询记录数=" + count);

System.out.println("增强for查询耗时:" + (startTimeFor - System.currentTimeMillis()));

}

@Test

public void iteratorQuery() {

int count = 0;

Map map = new HashMap();

for (int i = 0; i < 1000000; i++) {

map.put("key=" + i, "value=" + i);

}

Iterator> iterator = map.entrySet().iterator();

long startTimeIterator = System.currentTimeMillis();

while (iterator.hasNext()) {

String key = iterator.next().getKey();

String value = iterator.next().getValue();

System.out.println(key);

System.out.println(value);

count++;

}

System.out.println("查询记录数=" + count);

System.out.println("迭代器Iterator查询耗时:" + (startTimeIterator - System.currentTimeMillis()));

}

}

最后

以上就是柔弱冬日最近收集整理的关于php Foreach 和 迭代器,Iterator迭代器和foreach增强for循环的效率比较的全部内容,更多相关php内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(66)

评论列表共有 0 条评论

立即
投稿
返回
顶部