LinkedList inserts data efficiency compared with ArrayList

in conclusion

1 When inserting data at the end, LinkedList is faster when the amount of data is small, because ArrayList needs to be expanded frequently, and ArrayList is faster when the amount of data is large, because the expansion of ArrayList is 1.5 times the current capacity, and a large capacity expansion can provide a lot of space once. And LinkedList will add a new Node every time it is added. When the amount of data is large, it will take more time here.

2 When inserting data at the first part, LinkedList is faster, because LinkedList takes very little time to traverse the insertion position, while ArrayList needs to perform a System.arraycopy of all elements of the original array.

3 The more you insert into the middle, the lower the efficiency of LinkedList, because it traverses to obtain the insertion position from both ends to the middle, and the index traverses to the middle, the longer it is, so the insertion efficiency of ArrayList may be higher than that of LinkedList.

4 The further back the insertion position is, the higher the efficiency of ArrayList is, because the array needs to be copied and moved after less data, then System.arraycopy will be faster. Therefore, the efficiency of inserting data into LinkedList at the head is higher than that of ArrayList, and the efficiency of inserting data into ArrayList at the end is higher than that of LinkedList.

Insert data at the end

insert image description here

Conclusion: Tail insertion: ArrayList is better than LinkedList

first insert data

insert image description here

Conclusion: LinkedList is much faster than ArrayList for the first insertion

Insert data in the middle

insert image description here

Conclusion: Intermediate insertion, LinkedList is slower than ArrayList

When the amount of data is small, insert data in the middle, LinkedList performance is greater than ArrayList

insert image description here

When the amount of data is small, the ArrayList needs to be expanded frequently, while the new Node in LinkedList's add() takes less time in comparison. The number of times to move the pointer is also less.

Guess you like

Origin blog.csdn.net/qq_36256590/article/details/132356954