Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Performance 迭代器级联性能_Performance_Algorithm_Scala_Iterator - Fatal编程技术网

Performance 迭代器级联性能

Performance 迭代器级联性能,performance,algorithm,scala,iterator,Performance,Algorithm,Scala,Iterator,下面是迭代器++方法的代码: /** Concatenates this iterator with another. * * @param that the other iterator * @return a new iterator that first yields the values produced by this * iterator followed by the values produced by it

下面是
迭代器
++
方法的代码:

/** Concatenates this iterator with another.
       *
       *  @param   that   the other iterator
       *  @return  a new iterator that first yields the values produced by this
       *  iterator followed by the values produced by iterator `that`.
       *  @note    Reuse: $consumesTwoAndProducesOneIterator
       *  @usecase def ++(that: => Iterator[A]): Iterator[A]
       */
      def ++[B >: A](that: => GenTraversableOnce[B]): Iterator[B] = new Iterator[B] {
        // optimize a little bit to prevent n log n behavior.
        private var cur : Iterator[B] = self
        // since that is by-name, make sure it's only referenced once -
        // if "val it = that" is inside the block, then hasNext on an empty
        // iterator will continually reevaluate it.  (ticket #3269)
        lazy val it = that.toIterator
        // the eq check is to avoid an infinite loop on "x ++ x"
        def hasNext = cur.hasNext || ((cur eq self) && {
          it.hasNext && {
            cur = it
            true
          }
        })
        def next() = { hasNext; cur.next() }
      }
在注释中,它说:
//稍微优化一下,以防止n log n行为。


连接两个迭代器何时以及如何导致n log n

根据大众的要求,我引用上面的@Paolo Falabella评论回答了我自己的问题:

“Scala第二版编程”中提到了这一点。日志n是由于 每一步都必须做出决定而引入的额外间接性 下一个元素来自第一个或第二个元素时的迭代 迭代器


“Scala第二版编程”中提到了这一点。log n是由于在迭代的每一步都必须确定下一个元素是来自第一个迭代器还是第二个迭代器而引入的额外间接性造成的。如果始终执行对哪个迭代器为空的检查,那么通过级联迭代器进行级联,您会得到一个糟糕的复杂性,通过将新值重新指定给
cur
非常感谢:)解决了这一问题。这非常清楚。请您创建一个要接受的答案,以便该问题不再出现在未回答列表中?您可以回答自己的问题Mik:)