简体   繁体   中英

How to increase performance of a doubly linked list in java?

So I have an assignment that asks me to modify a method that traverses a doubly linked list. I got it to work, but our prof. set it up in a way that you can only receive full marks if the methods work faster than the one he provided. The list is 10000 elements.

This is how the original traverses the list:

private Node getNodeAt(int givenPosition)
{
    Node currentNode = firstNode;

    for(int counter = 1; counter < givenPosition; counter++)
    {
        currentNode = currentNode.next;
    }

    return currentNode;

this takes about 1331 milliseconds. This is one way we were told to traverse the list:

    private Node getNodeAt2(int givenPosition)
    {
     Node currentNode = firstNode;
     middleNode = firstNode;
     boolean nearFront = givenPosition <= (numberOfEntries / 4);
     boolean nearFrontMid = (givenPosition < (numberOfEntries / 2)) && (givenPosition > (numberOfEntries / 4));
     boolean nearBackMid = (givenPosition >= (numberOfEntries / 2)) && (givenPosition < ((3*numberOfEntries) / 4));
     boolean nearLast = (givenPosition >= ((3*numberOfEntries) / 4));

     middlePosition = numberOfEntries / 2;

     for(int counter = 1; counter < middlePosition; counter++)
     {
      middleNode = middleNode.next;
     }

     if(nearFront){
       currentNode = firstNode;
     for(int counter = 1; counter < givenPosition; counter++)
        {
         currentNode = currentNode.next;
        }
     }
     if(nearFrontMid){
       currentNode = middleNode;
       for(int counter = 1; counter <= (middlePosition - givenPosition) ; counter++)
         {
         currentNode = currentNode.previous;
         }

     }
     if(nearBackMid){
       currentNode = middleNode;
       for(int counter = 1; counter <= (givenPosition - middlePosition); counter++)
        {
        currentNode = currentNode.next;
        }

     }
     if (nearLast){
       currentNode = lastNode;
       for(int counter = 1; counter <= (numberOfEntries - givenPosition); counter++)
     {
      currentNode = currentNode.previous;
     }
  }

    return currentNode;

This one takes about 800 milliseconds, and even though it is faster, he expects it to take a quarter of the time the first method takes. Is that possible?

Some Thoughts:

1) In the second solution the middleNode is always searched, but it isn't needed every time (especially at the if-parts nearfront and nearlast ). It will be faster if the middleNode is just determined when it's needed.

2) The iteration is faster because the method knows where the middleNode is. Maybe "quaterNodes" help.

3) Sometimes recurrsive iterations are faster than a loop. Instead of looping call a method which is 1 step closer to the needed position. With 10000-elements there won't be any limit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM