当前位置:网站首页>Efficiency test of adding and querying ArrayList and LinkedList
Efficiency test of adding and querying ArrayList and LinkedList
2022-06-30 04:37:00 【Beginner 891】
Single column set ArrayList And LinkedList ArrayList Array set Quick query Adding and deleting intermediate elements is inefficient LinkedList Linked list set Slow query Additions and deletions quickly Here is a program to test ArrayList And LinkedList Insert 、 Query time efficiency , As shown below :
import java.util.ArrayList;
import java.util.LinkedList;
/**
* @author bzc
* @date 2022/1/25
* @apiNote ArrayList & LinkedList Adding and querying time-consuming tests
*/
public class ArrayListVSLinkList {
public static void main(String[] args) {
/**
* Test insertion time
*/
ArrayList<Integer> arrayListInsert = new ArrayList<>();
LinkedList<Integer> linkedListInsert = new LinkedList<>();
for (int i = 0; i < 100000; i++) {
arrayListInsert.add(i);
linkedListInsert.add(i);
}
//Arraylist Insertion time
Long arrayListStart1 = System.currentTimeMillis();//currentTimeMillis() What we get is from 1970-1-01 00:00:00.000 Time distance difference from the current time , The type is long
for (int i = 0; i < 100000; i++) {
arrayListInsert.add(1000, i);
}
Long arrayListEnd1 = System.currentTimeMillis();
System.out.println("ArrayList Insertion time :"+(arrayListEnd1 - arrayListStart1)+"ms");
//LinkedList Insertion time
Long linkedListStart1 = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
linkedListInsert.add(1000, i);
}
Long linkedListEnd1 = System.currentTimeMillis();
System.out.println("LinkedList Insertion time :"+(linkedListEnd1 - linkedListStart1)+"ms");
System.out.println("———————————————————————————————");
/**
* Test query time
*/
ArrayList<Integer> arrayListFind = new ArrayList<>();
LinkedList<Integer> linkedListFind = new LinkedList<>();
for (int i = 0; i < 100000; i++) {
arrayListFind.add(i);
linkedListFind.add(i);
}
//ArrayList Query time consuming
long arrayListStart2 = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
arrayListFind.get(5000);
}
long arrayListEnd2 = System.currentTimeMillis();
System.out.println("ArrayList Query time :"+(arrayListEnd2 - arrayListStart2)+"ms");
//LinkedList Query time consuming
long linkedListStart2 = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
linkedListFind.get(5000);
}
long linkedListEnd2 = System.currentTimeMillis();
System.out.println("LinkedList Query time :"+(linkedListEnd2 - linkedListStart2)+"ms");
}
}
Here are the results :

In a lot of times , Insertion time , LinkedList In terms of time efficiency, it is much better than ArrayList ; And the query takes time , ArrayList In terms of time efficiency, it is much better than LinkedList.
边栏推荐
- Memorize unfamiliar words at SSM stage and update them from time to time
- Mongodb learning
- 进程间通信之匿名管道
- 【Paper】2019_ Consensus Control of Multiple AUVs Recovery System Under Switching Topologies and Time D
- Myrpc version 0
- FortiGate performs DNAT mapping, and intranet users cannot normally access the mapping
- IO stream, buffer stream, automatic resource management
- [control] multi agent system summary. 4. control agreement.
- Configure specific source IP in SLA detection of FortiGate sdwan
- What is the difference between synchronized and lock
猜你喜欢

Junior students summarize JS advanced interview questions

How to use div boxes to simulate line triangles

管道实现进程间通信之命名管道

【Paper】2016_ A Learning-Based Fault Tolerant Tracking Control of an Unmanned Quadrotor Helicopter

FortiGate firewall configuration log uploading regularly

With the deep integration of cloud platform, the "Xueba" objectscale in the object storage industry is coming

Bean creation process and lazy init delay loading mechanism

Myrpc version 2

Redis cache avalanche, breakdown and penetration

Redis implements SMS login function (II) redis implements login function
随机推荐
Myrpc version 1
Differences between beanfactory and factorybean
Requirements for transfer transaction cases: 1 Employee 1 transfers money to employee 2. Therefore, two update sals should be executed. Purpose: either both updates are successful or both implementati
Mongodb learning
Myrpc version 0
SQL error caused by entity class: Oracle "ora-00904" error: possible case of invalid identifier
FortiGate firewall configuration log uploading regularly
Method of applying for code signing certificate by enterprise
Blocking queue example
Imile uses Zadig's multi cloud environment to deploy thousands of times a week to continuously deliver global business across clouds and regions
Difference between TCP three handshakes and four waves and tcp/udp
FortiGate performs DNAT mapping, and intranet users cannot normally access the mapping
Servlet lifecycle
Es2017 key summary
JS deconstruction assignment
Use of thread pool
Qt Creator 8 Beta2发布
System programming summary
破局存量客群营销,试一下客户分群管理(含聚类模型等实操效果评估)
JS inheritance