当前位置:网站首页>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 Oslo: Li Meng | deep reinforcement learning based on swing transformer
- Leetcode-934: the shortest Bridge
- Teach you JDBC hand in hand -- structure separation
- 1.12 - Instructions
- 机器学习:numpy版本线性回归预测波士顿房价
- logback配置文件
- Solution to the problem of abnormal display of PDF exported Chinese documents of confluence
- leetcode-2280:表示一个折线图的最少线段数
- 微信小程序获取某个元素的信息(高、宽等),并将px转换为rpx。
- Rust ownership (very important)
猜你喜欢

How SQLSEVER removes data with duplicate IDS

【AutoSAR 十一 通信相关机制】

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

AttributeError: ‘tuple‘ object has no attribute ‘layer‘问题解决

leetcode-849:到最近的人的最大距离

2022 list of manufacturers of Chinese 3D vision enterprises (guided positioning and sorting scenes)

深度剖析数据在内存中的存储
![[applet project development -- JD mall] user defined search component of uni app (middle) -- search suggestions](/img/ea/ee1ad50a497478b9d080bb5e4bdfb5.png)
[applet project development -- JD mall] user defined search component of uni app (middle) -- search suggestions

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

【AutoSAR 九 C/S原理架构】
随机推荐
Nacos+openfeign error reporting solution
Shell 实现文件基本操作(切割、排序、去重)
In the first half of 2022, there are 10 worth seeing, and each sentence can bring you strength!
Logback configuration file
Vulkan is not a "panacea"“
2022 list of manufacturers of Chinese 3D vision enterprises (guided positioning and sorting scenes)
lex && yacc && bison && flex 配置的问题
【AutoSAR 五 方法论】
About qbytearray storage hexadecimal and hexadecimal conversion
关于QByteArray存储十六进制 与十六进制互转
Use Jenkins II job
node_modules删不掉
Leetcode 294. Flip game II (game theory)
MySQL multi table joint deletion
2022上半年值得被看见的10条文案,每一句都能带给你力量!
Set up nacos2 X cluster steps and problems encountered
Leetcode-849: maximum distance to the nearest person
Leetcode-2115: find all the dishes that can be made from the given raw materials
图解网络:什么是虚拟路由器冗余协议 VRRP?
文件操作IO-Part2