网站首页 > 博客文章 正文
我们在开发中循环遍历一个数组经常会用到,jdk8推出了一些新特性,对循环做了比较,通过代码亲测,记录一下!
1、for循环
public static void main(String[] args) {
Long startTime = System.currentTimeMillis();
formMethod();
Long endTime = System.currentTimeMillis();
System.out.println("result:" + (endTime - startTime));
}
public static void formMethod(){
for (int i = 0; i < 10000; i++) {
System.out.println("start::::::::::::");
}
}
2、foreach循环(for循环的增强版)
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 10000; i++) {
list.add(i);
}
Long startTime = System.currentTimeMillis();
foreachMethod(list);
Long endTime = System.currentTimeMillis();
System.out.println("result:" + (endTime - startTime));
}
/**
* foreach
* @param list
*/
public static void foreachMethod(List<Integer> list){
list.forEach(i ->{
System.out.println("++++++++++++");
});
}
结论:
1、通过代码测试发现在1万以内的数据,for循环比foreach效率要高一些;
但是10万以内数据的时候,foreach效率更高一些;
2、foreach [10万数据时间 1112 1165 1203 1115] [1万数据 235 146 176 164 175];
3、for循环 [10万数据时间 1330 1437 1347] [1万数据 110 109 141];
3、stream api
(1)、串行处理,即同步处理
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 10000; i++) {
list.add(i);
}
Long startTime = System.currentTimeMillis();
streamMethod(list);
Long endTime = System.currentTimeMillis();
System.out.println("result:" + (endTime - startTime));
}
/**
* stream 串行处理
* @param list
*/
public static void streamMethod(List<Integer> list){
list.stream().forEach(i ->{
System.out.println("========");
});
}
结论:
1、1万以内的数据,for循环的性能要高于foreach和stream;
2、10万以内的数据明显可以看出stream效率最高,其次是foreach,最后是for;
3、[10万数据时间 854 892 789 844][1万数据 172 156 219 172 171];
(2)并行处理,即stream api提供了异步处理机制
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 10000; i++) {
list.add(i);
}
Long startTime = System.currentTimeMillis();
parallelStreamMethod(list);
Long endTime = System.currentTimeMillis();
System.out.println("result:" + (endTime - startTime));
}
/**
* stream 并行处理
* @param list
*/
public static void parallelStreamMethod(List<Integer> list){
list.parallelStream().forEach(i ->{
System.out.println("========");
});
}
结论:
1、1万以内的数据,for循环的性能要高于foreach和stream;
2、10万以内的数据明显可以看出stream效率最高,其次foreach,最后是for。
3、[10万数据时间 893 844 914 972][1万数据 219 203 234 188 ]
最终总结:
1、如果数据在1万以内的话,for循环效率高于foreach和stream;
2、如果数据量在10万的时候,stream效率最高,其次是foreach,最后是for。
3、另外需要注意的是如果数据达到100万的话,parallelStream异步并行处理效率最高,高于foreach和for。
猜你喜欢
- 2024-12-26 Java 8 Stream 处理大数据集:实战与优化
- 2024-12-26 面试官:Java8 lambda 表达式 forEach 如何提前终止?
- 2024-12-26 Javascript中,forEach和map到底有什么区别?
- 2024-12-26 Excel VBA之For Each遍历循环的应用
- 2024-12-26 为什么建议使用 for…of 循环而不是 foreach 循环呢
- 2024-12-26 前端开发map和foreach区别,map遍历方式用法介绍
- 2024-12-26 Rust语言从入门到精通系列 - 零基础掌握Stream流迭代器
- 2024-12-26 Map遍历的四种方法效率对比
- 2024-12-26 java集合类之java中集合类有哪些?如何分类?
- 2024-12-26 【一分钟学Java】之List
你 发表评论:
欢迎- 最近发表
-
- Python 中 必须掌握的 20 个核心函数—len()函数
- 用PLC的指针实现字符串转byte(Codesys平台)一文极简搞懂指针
- EXCEL如何用函数读取复杂字符串中的数据
- 2025-07-19:计算字符串的镜像分数。用go语言,给定一个字符串 s
- 2025-07-10:字符相同的最短子字符串Ⅰ。用go语言,给定一个长度
- 基于物理特征融合与机器学习的多井协同钻井速率实时预测与优化(
- [电子学报文章精选]一种基于特征融合的恶意代码快速检测方法
- 强大的可视化流程图编辑神器 — LogicFlow
- 前端框架太卷了!字节企业级框架Arco Design Mobile开源了
- Vue独立组件——11个最佳Vue.js日期选择器组件
- 标签列表
-
- ifneq (61)
- 字符串长度在线 (61)
- googlecloud (64)
- flutterrun (59)
- powershellfor (73)
- messagesource (71)
- plsql64位 (73)
- vueproxytable (64)
- npminstallsave (63)
- promise.race (63)
- 2019cad序列号和密钥激活码 (62)
- window.performance (66)
- qt删除文件夹 (72)
- mysqlcaching_sha2_password (64)
- nacos启动失败 (64)
- ssh-add (70)
- yarnnode (62)
- abstractqueuedsynchronizer (64)
- source~/.bashrc没有那个文件或目录 (65)
- springboot整合activiti工作流 (70)
- jmeter插件下载 (61)
- 抓包分析 (60)
- idea创建mavenweb项目 (65)
- qcombobox样式表 (68)
- pastemac (61)
本文暂时没有评论,来添加一个吧(●'◡'●)