当前位置:网站首页>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 .
边栏推荐
- leetcode-934:最短的桥
- 【AutoSAR 一 概述】
- Advanced pointer (I)
- University of Toronto: Anthony coach | the conditions of deep reinforcement learning can induce dynamic risk measurement
- Logback configuration file
- MySQL 23 classic interview hanging interviewer
- Linux软件:如何安装Redis服务
- 关于XML一些介绍和注意事项
- 研发一款国产ARM智能边缘计算网关需要什么
- Extension of flutter
猜你喜欢
【AutoSAR 五 方法论】

Hdu3507 (slope DP entry)

Basic use of shell script

Is there a free text to speech tool to help recommend?

antv x6节点拖拽到画布上后的回调事件(踩大坑记录)

AEM: Nanlin fan Ben et al. - plant rhizosphere growth promoting bacteria control soybean blight

【AutoSAR 十一 通信相关机制】

字符设备注册常用的两种方法和步骤

FAQ | FAQ for building applications for large screen devices

Solution to the problem of abnormal display of PDF exported Chinese documents of confluence
随机推荐
Teach you JDBC hand in hand -- structure separation
Two common methods and steps of character device registration
Vulkan performance and refinement
How to find out the currently running version of Solr- How do I find out version of currently running Solr?
Set up nacos2 X cluster steps and problems encountered
Vulkan-性能及精细化
【雅思阅读】王希伟阅读P1(阅读判断题)
1.12 - Instructions
测试右移:线上质量监控 ELK 实战
Automated defect analysis in electron microscopic images-论文阅读笔记
【JetCache】JetCache的配置说明和注解属性说明
指针初阶(基础)
【AutoSAR 十二 模式管理】
Unity learns from spaceshooter to record the difference between fixedupdate and update in unity for the second time
In the first half of 2022, there are 10 worth seeing, and each sentence can bring you strength!
Extension of flutter
【AutoSAR 九 C/S原理架构】
[MCU project training] eight way answering machine
使用jenkins之二Job
【日常训练】871. 最低加油次数