当前位置:网站首页>数组与集合性能比较
数组与集合性能比较
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级看不出性能差距,可以选择集合存放,因为有大量接口方法,方便调用。
对性能斤斤计较的可以选择基础类型数组。
边栏推荐
- Graduation summary
- [shutter] image component (load network pictures | load static pictures | load local pictures | path | provider plug-in)
- University of Oslo: Li Meng | deep reinforcement learning based on swing transformer
- 微信小程序获取某个元素的信息(高、宽等),并将px转换为rpx。
- NC50965 Largest Rectangle in a Histogram
- How to find out the currently running version of Solr- How do I find out version of currently running Solr?
- DotNet圈里一个优秀的ORM——FreeSql
- LeedCode1480. Dynamic sum of one-dimensional array
- Basic use of shell script
- One of the reasons why setinterval timer does not take effect in ie: the callback is the arrow function
猜你喜欢

One of the reasons why setinterval timer does not take effect in ie: the callback is the arrow function

【AutoSAR 四 BSW概述】

【AutoSAR 六 描述文件】
![[shutter] image component (load network pictures | load static pictures | load local pictures | path | provider plug-in)](/img/7e/4f9d96edd04e9ffb26434baf34aa43.jpg)
[shutter] image component (load network pictures | load static pictures | load local pictures | path | provider plug-in)

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

Unity learns from spaceshooter to record the difference between fixedupdate and update in unity for the second time

UART、RS232、RS485、I2C和SPI的介绍

redis21道经典面试题,极限拉扯面试官

Nacos+openfeign error reporting solution
![[MCU project training] eight way answering machine](/img/a3/6a50619cd16269bf485a4a273677aa.jpg)
[MCU project training] eight way answering machine
随机推荐
可下载《2022年中国数字化办公市场研究报告》详解1768亿元市场
There is an unknown problem in inserting data into the database
在线预览Word文档
1.12 - 指令
Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
关于QByteArray存储十六进制 与十六进制互转
University of Toronto: Anthony coach | the conditions of deep reinforcement learning can induce dynamic risk measurement
【AutoSAR 九 C/S原理架构】
pageoffice-之bug修改之旅
程序分析与优化 - 9 附录 XLA的缓冲区指派
[MCU project training] eight way answering machine
【AutoSAR 一 概述】
NC50965 Largest Rectangle in a Histogram
Explain in detail the significance of the contour topology matrix obtained by using the contour detection function findcontours() of OpenCV, and how to draw the contour topology map with the contour t
【AutoSAR 二 AppL概述】
How to find out the currently running version of Solr- How do I find out version of currently running Solr?
【案例分享】让新时代教育发展与“数”俱进
Callback event after the antv X6 node is dragged onto the canvas (stepping on a big hole record)
Automated defect analysis in electronic microscopic images
Form form instantiation