当前位置:网站首页>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 .
边栏推荐
- University of Toronto:Anthony Coache | 深度强化学习的条件可诱导动态风险度量
- 机器学习:numpy版本线性回归预测波士顿房价
- Shell 实现文件基本操作(切割、排序、去重)
- Leetcode-871: minimum refueling times
- leetcode-1964:找出到每个位置为止最长的有效障碍赛跑路线
- leetcode-849:到最近的人的最大距离
- Leetcode-934: the shortest Bridge
- 【日常训练】871. 最低加油次数
- 使用jenkins之二Job
- Unity learns from spaceshooter to record the difference between fixedupdate and update in unity for the second time
猜你喜欢
AEM: Nanlin fan Ben et al. - plant rhizosphere growth promoting bacteria control soybean blight
Liad: the consumer end of micro LED products is first targeted at TVs above 100 inches. At this stage, it is still difficult to enter a smaller size
Linux软件:如何安装Redis服务
【AutoSAR 一 概述】
ftrace工具的介绍及使用
为什么网站打开速度慢?
[applet project development -- JD mall] user defined search component of uni app (middle) -- search suggestions
Why is the website slow to open?
深度剖析数据在内存中的存储
研发一款国产ARM智能边缘计算网关需要什么
随机推荐
Shell implements basic file operations (cutting, sorting, and de duplication)
[IELTS reading] Wang Xiwei reading P2 (reading fill in the blank)
腾讯云免费SSL证书扩展文件含义
lex && yacc && bison && flex 配置的問題
Solution to the problem of abnormal display of PDF exported Chinese documents of confluence
Shell 实现文件基本操作(sed-编辑、awk-匹配)
Nacos+openfeign error reporting solution
Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
Machine learning: numpy version linear regression predicts Boston house prices
Attributeerror: 'tuple' object has no attribute 'layer' problem solving
1.11 - 总线
1.11 - bus
Vulkan is not a "panacea"“
【Pulsar文档】概念和架构/Concepts and Architecture
文件操作IO-Part2
Baidu AI Cloud takes the lead in building a comprehensive and standardized platform for smart cloud
About qbytearray storage hexadecimal and hexadecimal conversion
【雅思阅读】王希伟阅读P1(阅读判断题)
数组与集合性能比较
【AutoSAR 三 RTE概述】