JDK8-lambda表达式四种forEach性能对比
jdk8新特性流Stream编程
看了网上一些性能的比较,这里自己写一个进行测试
对比以下四种
普通forEach、java8中新的forEach、stream+forEach、parallelStream+forEach
package com.huajie.Lambda;
import lombok.extern.slf4j.Slf4j;
import org.junit.Before;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
@Slf4j
public class Mode2 {
List<User> listUser = new ArrayList<>();
@Before
public void initList() {
listUser = this.getListUsers();
}
@Test
public void test() {
//一般forEach
long startSimpleTime = System.currentTimeMillis();
for (User user : listUser) {
user.toString();
}
long endSimpleTime = System.currentTimeMillis();
System.out.println("Simple:" + (endSimpleTime - startSimpleTime));
//java8中新的forEach
long startLambda = System.currentTimeMillis();
listUser.forEach(User::toString);
long endLambda = System.currentTimeMillis();
System.out.println("Lambda:" + (endLambda - startLambda));
//java8中新的stream+forEach
long startStream = System.currentTimeMillis();
listUser.stream().forEach(User::toString);
long endStream = System.currentTimeMillis();
System.out.println("Stream:" + (endStream - startStream));
//java8中新的parallelStream+forEach
long startParallelStream = System.currentTimeMillis();
listUser.parallelStream().forEach(User::toString);
long endParallelStream = System.currentTimeMillis();
System.out.println("ParallelStream:" + (endParallelStream - startParallelStream));
}
private List<User> getListUsers() {
List<User> listUser = new ArrayList<User>();
for (int i = 0; i < 10000000; i++) {
listUser.add(new User("user" + i, i));
}
return listUser;
}
}
package com.huajie.Lambda;
import lombok.Data;
@Data
public class User {
private String name;
private Integer age;
public User(String name, Integer age) {
this.name = name;
this.age = age;
}
}
1000W条数据
100W条数据
10W条数据
1W条数据
1000条数据
java8中新加入的forEach和普通的forEach临界值大概在150W条
由以上测试能得到的结果:
150W条数据以下and10W以上:lambda>simple>stream>paralleStream
150W条数据以上:simple>lambda>stream>paralleStream
从性能上考虑:小数据量用普通的forEach就可以,没有必要使用java8中的新出来的几种,已经在项目中使用的也不需要改回来,10W条也就几毫秒的差距
————————————————
版权声明:本文为CSDN博主「不懂的浪漫」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/xiewenfeng520/article/details/88724349
JDK8-lambda表达式四种forEach性能对比相关推荐
- PHP5.5四种序列化性能对比
2019独角兽企业重金招聘Python工程师标准>>> 结论: 1.小数组用msgpack,无论空间和性能都最好 2.大数组,考虑空间用igbinary,考虑性能用msgpack j ...
- 三星note5 android版本区别吗,三星Note5哪种颜色好看?三星Note5四种颜色区别对比图解...
三星Note5有几种颜色?哪种颜色更好看呢?三星Note5是一款时下非常受欢迎的大屏旗舰手机,搭载Exynos 7422八核处理器,4GB超大内存,配备S Pen触控笔,支持指纹识别等特性,颇受消费者 ...
- linux数据同步技术比较,linux下实现web数据同步的四种方式(性能比较)教程.docx
linux下实现web数据同步的四种方式(性能比较)教程 实现web数据同步的四种方式=======================================1.nfs实现web数据共享2.rs ...
- Siege压力测试工具的安装及使用+python flask的四种wsgi方式对比
文章目录 一.前言: 如果要支持https 二.安装使用: 文件备份: 1.mac安装: 2.linux 安装:[centos 服务器] 通用Linux安装: 3.window安装: 4.测试百度: ...
- python dict遍历文件_python 字典(dict)遍历的四种方法性能测试报告
python中,遍历dict的方法有四种.但这四种遍历的性能如何呢?我做了如下的测试 l = [(x,x) for x in xrange(10000)]d = dict(l)from time im ...
- python dict遍历_python 字典(dict)遍历的四种方法性能测试报告
python中,遍历dict的方法有四种.但这四种遍历的性能如何呢?我做了如下的测试 l = [(x,x) for x in xrange(10000)] d = dict(l) from time ...
- python dict遍历性能_python 字典(dict)遍历的四种方法性能测试报告
python中,遍历dict的方法有四种.但这四种遍历的性能如何呢?我做了如下的测试 l = [(x,x) for x in xrange(10000)] d = dict(l) from time ...
- linux下实现web数据同步的四种方式(性能比较)
实现web数据同步的四种方式 ======================================= 1.nfs实现web数据共享 2.rsync +inotify实现web数据同步 3.rs ...
- java 数组效率_java数组复制的四种方法效率对比
有关数组的基础知识,有很多方面,比方说初始化,引用,遍历,以及一维数组和二维数组,今天我们先看看数组复制的有关内容. 来源于牛客网的一道选择题: JAVA语言的下面几种数组复制方法中,哪个效率最高? ...
最新文章
- Nature:剖腹产到底好不好?——肠道菌群的视角
- Python: UTF8转换代码实例
- bootstrap中点击左边展开
- DOM中的setInterval方法
- 菠萝蜜\菠萝蜜和榴莲有什么区别?
- linux fedora35 zsh oh-my-zsh 的配置与使用
- rpc结构错误_结构性错误
- leetcode 477. 汉明距离总和(位运算)
- 揭秘一线互联网企业 前端JavaScript高级面试
- ubuntu启动脚本的一些笔记
- Variant 数组
- Mac苹果Time Machine 备份速度太慢的解决方法!
- 基于java+SpringBoot+HTML+Mysq幼儿园日常管理系统
- 力扣(leetcode)67. 二进制求和——c语言新手入门
- SpringBoot项目实战,附源码
- 【ELM预测】基于蝴蝶算法优化极限学习机预测附matlab代码
- Redis的优点和缺点
- Microsoft Toolkit(win8.1激活工具)v2.5.2 绿色版
- 如何用爬虫工具实现竞对价格监控
- 二十九 我在软件园的那些日子里
热门文章
- java桥接和装饰_设计模式:桥接模式和装饰模式
- java 云 代码_云端如何编写Java代码
- vue require动态路径图片报错_Vue 动态生成路由结构
- 65.Java语法糖
- 27、Python 面向对象(创建类、创建实例对象、访问属性、内置类属性、对象销毁、类的继承、方法重写、基础重载方法、运算符重载、类属性与方法、下划线双下划线)
- 1高并发服务器:多进程服务器
- 【tensorflow】tf.layers.conv1d函数解析(一维卷积)
- stm32 无符号整形_十进制字符串转化为无符号整数
- C语言中编译、生成、调试、运行的区别
- 前端性能优化之防抖-debounce