当前位置:网站首页>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 .
边栏推荐
猜你喜欢

Kubernetes resource object introduction and common commands (V) - (NFS & PV & PVC)

Callback event after the antv X6 node is dragged onto the canvas (stepping on a big hole record)

FAQ | FAQ for building applications for large screen devices

1.11 - 总线

Basic use of shell script

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

Leetcode-2280: represents the minimum number of line segments of a line graph

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

File operation io-part2

世平信息首席科学家吕喆:构建以数据和人员为中心的安全能力
随机推荐
Vulkan-实践第一弹
[daily training] 871 Minimum refueling times
字符设备注册常用的两种方法和步骤
One of the reasons why setinterval timer does not take effect in ie: the callback is the arrow function
mysql 多表联合删除
腾讯云免费SSL证书扩展文件含义
研发一款国产ARM智能边缘计算网关需要什么
tail -f 、tail -F、tailf的区别
关于QByteArray存储十六进制 与十六进制互转
Tensorflow 2.x(keras)源码详解之第十五章:迁移学习与微调
机器学习:numpy版本线性回归预测波士顿房价
[jetcache] jetcache configuration description and annotation attribute description
Markdown tutorial
[pulsar document] concepts and architecture
kubernetes编写yml简单入门
Attributeerror: 'tuple' object has no attribute 'layer' problem solving
Andorid gets the system title bar height
Rust string slicing, structs, and enumeration classes
Why is the website slow to open?
leetcode-934:最短的桥