当前位置:网站首页>数组与集合性能比较
数组与集合性能比较
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级看不出性能差距,可以选择集合存放,因为有大量接口方法,方便调用。
对性能斤斤计较
的可以选择基础类型数组。
边栏推荐
- 图解网络:什么是虚拟路由器冗余协议 VRRP?
- Cordova plugin device obtains the device information plug-in, which causes Huawei to fail the audit
- University of Toronto:Anthony Coache | 深度强化学习的条件可诱导动态风险度量
- Multi process programming (III): message queue
- Shell implements basic file operations (cutting, sorting, and de duplication)
- 1.11 - bus
- Nc20806 District interval
- Free we media essential tools sharing
- 数组常用操作方法整理(包含es6)及详细使用
- NC24840 [USACO 2009 Mar S]Look Up
猜你喜欢
随机推荐
【AutoSAR 五 方法论】
图解网络:什么是虚拟路由器冗余协议 VRRP?
Shell implements basic file operations (cutting, sorting, and de duplication)
Is there a free text to speech tool to help recommend?
The "2022 China Digital Office Market Research Report" can be downloaded to explain the 176.8 billion yuan market in detail
【雅思阅读】王希伟阅读P2(阅读填空)
详解用OpenCV的轮廓检测函数findContours()得到的轮廓拓扑结构(hiararchy)矩阵的意义、以及怎样用轮廓拓扑结构矩阵绘制轮廓拓扑结构图
1.12 - 指令
Callback event after the antv X6 node is dragged onto the canvas (stepping on a big hole record)
Some introduction and precautions about XML
Leetcode 294. Flip game II (game theory)
Shell 实现文件基本操作(sed-编辑、awk-匹配)
简单聊聊运维监控的其他用途
【luogu P4320】道路相遇(圆方树)
Unity learns from spaceshooter to record the difference between fixedupdate and update in unity for the second time
NC17059 队列Q
Shell 实现文件基本操作(切割、排序、去重)
There is an unknown problem in inserting data into the database
关于QByteArray存储十六进制 与十六进制互转
Cordova plugin device obtains the device information plug-in, which causes Huawei to fail the audit