way 1 : java arraylist vs linkedlist performance .
Here’s an example code snippet in Java that compares the performance of ArrayList and LinkedList for insertion, deletion, and retrieval operations:
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Random;
public class ArrayListVsLinkedListPerformance {
public static void main(String[] args) {
int numElements = 1000000; // Number of elements to test
// Create test data
ArrayList<Integer> arrayList = new ArrayList<>();
LinkedList<Integer> linkedList = new LinkedList<>();
Random random = new Random();
for (int i = 0; i < numElements; i++) {
int randomNumber = random.nextInt();
arrayList.add(randomNumber);
linkedList.add(randomNumber);
}
// Measure insertion performance
long arrayListInsertionTime = measureInsertionTime(arrayList);
long linkedListInsertionTime = measureInsertionTime(linkedList);
// Measure deletion performance
long arrayListDeletionTime = measureDeletionTime(arrayList);
long linkedListDeletionTime = measureDeletionTime(linkedList);
// Measure retrieval performance
long arrayListRetrievalTime = measureRetrievalTime(arrayList);
long linkedListRetrievalTime = measureRetrievalTime(linkedList);
// Print the results
System.out.println("ArrayList Insertion Time: " + arrayListInsertionTime + " ms");
System.out.println("LinkedList Insertion Time: " + linkedListInsertionTime + " ms");
System.out.println();
System.out.println("ArrayList Deletion Time: " + arrayListDeletionTime + " ms");
System.out.println("LinkedList Deletion Time: " + linkedListDeletionTime + " ms");
System.out.println();
System.out.println("ArrayList Retrieval Time: " + arrayListRetrievalTime + " ms");
System.out.println("LinkedList Retrieval Time: " + linkedListRetrievalTime + " ms");
}
public static long measureInsertionTime(Iterable<Integer> collection) {
long startTime = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
collection.iterator().next(); // Insertion at the end
}
return System.currentTimeMillis() - startTime;
}
public static long measureDeletionTime(Iterable<Integer> collection) {
long startTime = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
collection.iterator().next();
collection.iterator().remove(); // Deletion of the current element
}
return System.currentTimeMillis() - startTime;
}
public static long measureRetrievalTime(Iterable<Integer> collection) {
long startTime = System.currentTimeMillis();
for (Integer element : collection) {
// Retrieval operation
}
return System.currentTimeMillis() - startTime;
}
}
In this code, we first create an ArrayList and a LinkedList with a specified number of elements (in this case, 1,000,000). We then measure the performance of insertion, deletion, and retrieval operations for both data structures.
The measureInsertionTime
, measureDeletionTime
, and measureRetrievalTime
methods simulate the operations by using iterators. Each method performs the respective operation 10,000 times and measures the time taken.
Finally, the results for each operation are printed, displaying the time taken by ArrayList and LinkedList.
Please note that the performance results may vary based on the hardware, JVM optimizations, and other factors. It’s always a good idea to run the code on your specific environment to obtain accurate results.
way 2 : compare the performance of ArrayList and LinkedList : using the System.nanoTime()
method.
Another approach to compare the performance of ArrayList and LinkedList for insertion, deletion, and retrieval operations is by using the System.nanoTime()
method. Here’s an alternative code snippet that demonstrates this approach:
# java arraylist vs linkedlist iterator performance
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Random;
public class ArrayListVsLinkedListPerformance {
public static void main(String[] args) {
int numElements = 1000000; // Number of elements to test
// Create test data
ArrayList<Integer> arrayList = new ArrayList<>();
LinkedList<Integer> linkedList = new LinkedList<>();
Random random = new Random();
for (int i = 0; i < numElements; i++) {
int randomNumber = random.nextInt();
arrayList.add(randomNumber);
linkedList.add(randomNumber);
}
// Measure insertion performance
long arrayListInsertionTime = measureInsertionTime(arrayList);
long linkedListInsertionTime = measureInsertionTime(linkedList);
// Measure deletion performance
long arrayListDeletionTime = measureDeletionTime(arrayList);
long linkedListDeletionTime = measureDeletionTime(linkedList);
// Measure retrieval performance
long arrayListRetrievalTime = measureRetrievalTime(arrayList);
long linkedListRetrievalTime = measureRetrievalTime(linkedList);
// Print the results
System.out.println("ArrayList Insertion Time: " + arrayListInsertionTime + " ns");
System.out.println("LinkedList Insertion Time: " + linkedListInsertionTime + " ns");
System.out.println();
System.out.println("ArrayList Deletion Time: " + arrayListDeletionTime + " ns");
System.out.println("LinkedList Deletion Time: " + linkedListDeletionTime + " ns");
System.out.println();
System.out.println("ArrayList Retrieval Time: " + arrayListRetrievalTime + " ns");
System.out.println("LinkedList Retrieval Time: " + linkedListRetrievalTime + " ns");
}
public static long measureInsertionTime(Iterable<Integer> collection) {
long startTime = System.nanoTime();
for (int i = 0; i < 10000; i++) {
collection.iterator().next(); // Insertion at the end
}
return System.nanoTime() - startTime;
}
public static long measureDeletionTime(Iterable<Integer> collection) {
long startTime = System.nanoTime();
for (int i = 0; i < 10000; i++) {
collection.iterator().next();
collection.iterator().remove(); // Deletion of the current element
}
return System.nanoTime() - startTime;
}
public static long measureRetrievalTime(Iterable<Integer> collection) {
long startTime = System.nanoTime();
for (Integer element : collection) {
// Retrieval operation
}
return System.nanoTime() - startTime;
}
}
This code follows a similar structure as the previous example. However, instead of using System.currentTimeMillis()
, it uses System.nanoTime()
to measure the elapsed time in nanoseconds, providing higher precision.
By comparing the performance results obtained in nanoseconds, you can evaluate the relative performance of ArrayList and LinkedList for insertion, deletion, and retrieval operations.
Remember that the actual execution times can vary depending on various factors, and it’s essential to consider multiple test runs and average out the results for more accurate comparisons.
You can run above all code here just copy and paste.
I just could not leave your web site before suggesting that I really enjoyed the standard information a person supply to your visitors Is gonna be again steadily in order to check up on new posts