简体   繁体   English

如何在Java中提高双向链表的性能?

[英]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. 该列表是10000个元素。

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. 这大约需要1331毫秒。 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. 这个过程大约需要800毫秒,尽管速度更快,但他预计这将花费第一种方法花费的时间的四分之一。 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 ). 1)在第二个解决方案中,总是搜索middleNode,但并非每次都需要搜索(尤其是在if-parts nearfront和nearlast)。 It will be faster if the middleNode is just determined when it's needed. 如果仅在需要时确定MiddleNode,它将更快。

2) The iteration is faster because the method knows where the middleNode is. 2)迭代速度更快,因为该方法知道中间节点在哪里。 Maybe "quaterNodes" help. 也许“ quaterNodes”的帮助。

3) Sometimes recurrsive iterations are faster than a loop. 3)有时循环迭代比循环快。 Instead of looping call a method which is 1 step closer to the needed position. 无需循环调用距离所需位置更近1步的方法。 With 10000-elements there won't be any limit. 具有10000个元素将没有任何限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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