当前位置:网站首页>数组与集合性能比较
数组与集合性能比较
2022-07-02 23:53:00 【凌康A14849】
数组与集合性能比较
数组与集合性能比较,集合对数据结构进行优化分类成:ArrayList、Map等等,那他们与基本类型的数组性能如何呢。看理论还不如时间一波:读、写两方面。
写入性能比较
public class ArrayTest {
public static void main(String[] args) {
// 对jvm、cup进行预热
int temp=0;
for (int i=0;i<1000000;i++){
temp+=i;
}
System.out.println(temp);
String[] arr = new String[1000000];
Collection<String> coll = new ArrayList<>();
long start = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
arr[i] = i + "";
}
System.out.println("数组100w赋值耗时:" + (System.currentTimeMillis() - start));
start = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
coll.add(i + "");
}
System.out.println("集合100w赋值耗时:" + (System.currentTimeMillis() - start));
}
}
结果:
1783293664
数组100w赋值耗时:39
集合100w赋值耗时:44
不管先后,正常写入还是基本类型的性能快
读取性能比较
public class ArrayTest02 {
public static void main(String[] args) {
// 对jvm、cup进行预热
int temp = 0;
for (int i = 0; i < 1000000; i++) {
temp += i;
}
System.out.println(temp);
String[] arr = new String[1000000];
Collection<String> coll = new ArrayList<>();
long start = 0;
start = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
coll.add(i + "");
}
System.out.println("集合100w赋值耗时:" + (System.currentTimeMillis() - start));
start = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
arr[i] = i + "";
}
System.out.println("数组100w赋值耗时:" + (System.currentTimeMillis() - start));
List<String> read = new ArrayList<>();
read.add("5");
read.add("999999");
read.add("439999");
read.add("666666");
start = System.currentTimeMillis();
for (String find : read)
if(coll.contains(find)){
temp++;
}
System.out.println("集合100w随机读取耗时:" + (System.currentTimeMillis() - start));
start = System.currentTimeMillis();
for (String find : read)
for (int i = 0; i < arr.length; i++) {
if (find.equals(arr[i])) {
temp++;
break;
}
}
System.out.println("数组100w随机读取耗时:" + (System.currentTimeMillis() - start));
}
}
结果一:
1783293664
集合100w赋值耗时:50
数组100w赋值耗时:59
集合100w随机读取耗时:15
数组100w随机读取耗时:13
结构二:
1783293664
集合100w赋值耗时:47
数组100w赋值耗时:61
数组100w随机读取耗时:16
集合100w随机读取耗时:15
不难看出,先后执行与jvm、CPU的调度有关,不管怎样,都是基本数组性能优先,毕竟集合底层就是数组实现
结论
数据量100w级看不出性能差距,可以选择集合存放,因为有大量接口方法,方便调用。
对性能斤斤计较的可以选择基础类型数组。
边栏推荐
- Solution to the problem of abnormal display of PDF exported Chinese documents of confluence
- Overlay of shutter (Pop-Up)
- 【AutoSAR 七 工具链简介】
- Shell 实现文件基本操作(切割、排序、去重)
- Linux Software: how to install redis service
- MySQL 23 classic interview hanging interviewer
- 【小程序项目开发-- 京东商城】uni-app之自定义搜索组件(中)-- 搜索建议
- 奥斯陆大学:Li Meng | 基于Swin-Transformer的深度强化学习
- 毕业总结
- NC24840 [USACO 2009 Mar S]Look Up
猜你喜欢

Logback configuration file

logback配置文件

Win10 多种方式解决无法安装.Net3.5的问题

Redis21 classic interview questions, extreme pull interviewer

可下载《2022年中国数字化办公市场研究报告》详解1768亿元市场

Confluence的PDF导出中文文档异常显示问题解决

【雅思阅读】王希伟阅读P1(阅读判断题)

University of Oslo: Li Meng | deep reinforcement learning based on swing transformer

图解网络:什么是虚拟路由器冗余协议 VRRP?

利亚德:Micro LED 产品消费端首先针对 100 英寸以上电视,现阶段进入更小尺寸还有难度
随机推荐
Array common operation methods sorting (including ES6) and detailed use
Graduation summary
1.12 - Instructions
免费自媒体必备工具分享
文件操作IO-Part2
可下载《2022年中国数字化办公市场研究报告》详解1768亿元市场
Wechat applet obtains the information of an element (height, width, etc.) and converts PX to rpx.
【AutoSAR 四 BSW概述】
Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
node_ Modules cannot be deleted
[pulsar document] concepts and architecture
UART、RS232、RS485、I2C和SPI的介绍
Shell implements basic file operations (cutting, sorting, and de duplication)
Nacos+openfeign error reporting solution
线程的启动与优先级
form表单实例化
NC20806 区区区间间间
Linux Software: how to install redis service
Andorid gets the system title bar height
为什么网站打开速度慢?