当前位置:网站首页>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.
边栏推荐
- Serializable and Deserialize
- 破局存量客群营销,试一下客户分群管理(含聚类模型等实操效果评估)
- [learn FPGA programming from scratch -52]: high level chapter - FPGA development based on IP core - basic framework for IP core use (taking PLL as an example)
- Differences between beanfactory and factorybean
- Software digital signature certificate
- FortiGate firewall filters the specified session and cleans it up
- Bean创建流程 与 lazy-init 延迟加载机制原理
- Myrpc version 0
- Use of thread pool
- System programming summary
猜你喜欢

【Paper】2017_ Distributed control for high-speed trains movements

Sectigo certificate

File and IO

Five methods to clear floating and their advantages and disadvantages

Redis cache avalanche, breakdown and penetration

Issue SSL certificate with IP address

Interview topic of MySQL

JS inheritance
![[UGV] schematic diagram of UGV version 32](/img/4b/03471d2cc96be5d57c97fd1c4e17dc.jpg)
[UGV] schematic diagram of UGV version 32

管道实现进程间通信之命名管道
随机推荐
Qt6 QML Book/Qt Quick 3D/Qt Quick 3D
Bean创建流程 与 lazy-init 延迟加载机制原理
The most comprehensive summary notes of redis foundation + advanced project in history
IIS request SSL certificate
进程间通信之匿名管道
Method of applying for code signing certificate by enterprise
Configure specific source IP in SLA detection of FortiGate sdwan
[从零开始学习FPGA编程-52]:高阶篇 - 基于IP核的FPGA开发 - IP核使用的基本框架(以锁相环PLL为例)
Array of small C
[FPGA] IIC读写EEPROM 的实现
FortiGate performs DNAT mapping, and intranet users cannot normally access the mapping
Detailed explanation of network layer
IO stream, buffer stream, automatic resource management
深度融合云平台,对象存储界的“学霸”ObjectScale来了
Sectigo certificate
史上最全的Redis基础+进阶项目实战总结笔记
【Paper】2013_ An efficient model predictive control scheme for an unmanned quadrotor helicopter
QT creator 8 beta2 release
JS file block to Base64 text
Myrpc version 5