当前位置:网站首页>Array and collection performance comparison
Array and collection performance comparison
2022-07-03 00:48:00 【Lingkang a14849】
Array and collection performance comparison
Array and collection performance comparison , The set optimizes and classifies the data structure into :ArrayList、Map wait , How about their performance with arrays of basic types . Time is better than theory : read 、 Write two aspects .
Write performance comparison
public class ArrayTest {
public static void main(String[] args) {
// Yes jvm、cup Preheat
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(" Array 100w Assignment time :" + (System.currentTimeMillis() - start));
start = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
coll.add(i + "");
}
System.out.println(" aggregate 100w Assignment time :" + (System.currentTimeMillis() - start));
}
}
result :
1783293664
Array 100w Assignment time :39
aggregate 100w Assignment time :44
Regardless of the order , Normal writing or basic type performance is fast
Reading performance comparison
public class ArrayTest02 {
public static void main(String[] args) {
// Yes jvm、cup Preheat
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(" aggregate 100w Assignment time :" + (System.currentTimeMillis() - start));
start = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
arr[i] = i + "";
}
System.out.println(" Array 100w Assignment time :" + (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(" aggregate 100w Random reading takes time :" + (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(" Array 100w Random reading takes time :" + (System.currentTimeMillis() - start));
}
}
Results a :
1783293664
aggregate 100w Assignment time :50
Array 100w Assignment time :59
aggregate 100w Random reading takes time :15
Array 100w Random reading takes time :13
Structure II :
1783293664
aggregate 100w Assignment time :47
Array 100w Assignment time :61
Array 100w Random reading takes time :16
aggregate 100w Random reading takes time :15
It's not hard to see. , Successively implement and jvm、CPU It's about scheduling , No matter what , Basic arrays are preferred , After all, the bottom layer of the set is the array implementation
Conclusion
Data volume 100w There is no performance gap at level , You can choose collection storage , Because there are a lot of interface methods , Convenient to call .
On Performance square accounts in every detail You can choose the basic type array .
边栏推荐
- 1.12 - 指令
- Array common operation methods sorting (including ES6) and detailed use
- Extension of flutter
- MySQL 23 classic interview hanging interviewer
- Some introduction and precautions about XML
- [Luogu p4320] road meets (round square tree)
- 1.11 - bus
- University of Toronto: Anthony coach | the conditions of deep reinforcement learning can induce dynamic risk measurement
- Teach you JDBC hand in hand -- structure separation
- Introduction and use of ftrace tool
猜你喜欢

世平信息首席科学家吕喆:构建以数据和人员为中心的安全能力

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

【AutoSAR 十一 通信相关机制】

The "2022 China Digital Office Market Research Report" can be downloaded to explain the 176.8 billion yuan market in detail

Linux Software: how to install redis service

1.11 - 总线

Gan model architecture in mm

Teach you JDBC hand in hand -- structure separation

瑞萨RZ/G2L 处理器简介|框架图|功耗|原理图及硬件设计指南

【案例分享】让新时代教育发展与“数”俱进
随机推荐
1.12 - Instructions
Tensorflow 2.x(keras)源码详解之第十五章:迁移学习与微调
深度剖析数据在内存中的存储
Redis21 classic interview questions, extreme pull interviewer
简单聊聊运维监控的其他用途
The difference between tail -f, tail -f and tail
指针初阶(基础)
Vulkan is not a "panacea"“
The "2022 China Digital Office Market Research Report" can be downloaded to explain the 176.8 billion yuan market in detail
Arduino开发之按键检测与正弦信号输出
【Pulsar文档】概念和架构/Concepts and Architecture
腾讯云免费SSL证书扩展文件含义
[IELTS reading] Wang Xiwei reading P1 (reading judgment question)
leetcode-934:最短的桥
瑞萨RZ/G2L 处理器简介|框架图|功耗|原理图及硬件设计指南
leetcode-871:最低加油次数
Helm basic learning
Leetcode-849: maximum distance to the nearest person
There is an unknown problem in inserting data into the database
【AutoSAR 九 C/S原理架构】