Linked list Performance issues [message #1516551] |
Thu, 18 December 2014 19:06  |
Eclipse User |
|
|
|
Simply put, using the linked list template in java they're performing worse than arrays on my machine, also my custom linked list implementation is performing up to 20-30 times worse than both the array and the template.
Simply i'm doing about 1 million iterations of traversing a an array/list and incrementing and integer.
The output is:
Linked List Template: 22501000
Linked List Duration: 368034000
Array Duration: 12162000
Array is faster by: 355872000seconds by 2900%.
The code:
main.java
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.Random;
public class main {
public static void main(String[] args)
{
long startTime = 0, endTime = 0, linkedListDuration = 0, arrayDuration = 0, templateDuration = 0;
Random random = new Random();
final int size = 1000000;
Integer lah;
LinkedList<Integer> linkedList = new LinkedList();
SuperLinkedList sll = null, head = null;
Integer[] superArray = new Integer[size];
for(int i = 0; i < size; i++)
{
if(head == null)
{
sll = new SuperLinkedList(random.nextInt());
head = sll;
}
else
{
sll.next = new SuperLinkedList(random.nextInt());
sll.next.previous = sll;
sll.next.next = null;
sll = sll.next;
}
superArray[i] = random.nextInt();
linkedList.add(new Integer(random.nextInt()));
}
//Time traversal of array
startTime = System.nanoTime();
for(int i = 0; i < size; i++)
{
lah = superArray[i];
lah++;
//System.out.println("Data is:" + superArray[i]);
}
endTime = System.nanoTime();
arrayDuration = endTime - startTime;
//Time traversal of Linked List Template
startTime = System.nanoTime();
Integer integer = null;
for(ListIterator<Integer> listIter = linkedList.listIterator(); listIter.hasNext();)
{
integer = listIter.next();
integer++;
//System.out.println("Data is:" + integer);
}
endTime = System.nanoTime();
templateDuration = endTime - startTime;
//Time traversal of LinkedList
startTime = System.nanoTime();
for(SuperLinkedList cll = head; cll.next != null;cll = cll.next)
{
//System.out.println("Data is: " + cll.integer);
cll.integer++;
}
endTime = System.nanoTime();
linkedListDuration = endTime - startTime;
System.out.println("Linked List Template: " + templateDuration);
System.out.println("Linked List Duration: " + linkedListDuration);
System.out.println("Array Duration: " + arrayDuration);
if(arrayDuration > linkedListDuration)
{
System.out.println("Linked List is faster by: " + (arrayDuration - linkedListDuration) +
"seconds by " + ((arrayDuration - linkedListDuration) / linkedListDuration * 100)
+ "%.");
}
else
{
System.out.println("Array is faster by: " + (linkedListDuration - arrayDuration) +
"seconds by " + ((linkedListDuration - arrayDuration) / arrayDuration * 100)
+ "%.");
}
}
}
SuperLinkedList.java
public class SuperLinkedList
{
public SuperLinkedList(Integer newInteger)
{
integer = newInteger;
}
public void increment()
{
integer++;
}
public Integer integer;
public SuperLinkedList previous, next;
}
|
|
|
|
|
Re: Linked list Performance issues [message #1519685 is a reply to message #1519329] |
Sat, 20 December 2014 15:41  |
Eclipse User |
|
|
|
Rephrasing David's second piece of advice: in the times of JIT compilers performing heuristic based optimizations, micro benchmarks like the one shown in this post must be interpreted with great caution. They usually don't give a lot of information regarding the performance of real world programs.
But again: this really isn't a question regarding the tool JDT.
|
|
|
Powered by
FUDForum. Page generated in 0.03666 seconds