aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/scala/spark/util/TimedIterator.scala
blob: 539b01f4ce47d3ff7237ca619d220aded7b04ee1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package spark.util

/**
 * A utility for tracking the total time an iterator takes to iterate through its elements.
 *
 * In general, this should only be used if you expect it to take a considerable amount of time
 * (eg. milliseconds) to get each element -- otherwise, the timing won't be very accurate,
 * and you are probably just adding more overhead
 */
class TimedIterator[+A](val sub: Iterator[A]) extends Iterator[A] {
  private var netMillis = 0l
  private var nElems = 0
  def hasNext = {
    val start = System.currentTimeMillis()
    val r = sub.hasNext
    val end = System.currentTimeMillis()
    netMillis += (end - start)
    r
  }
  def next = {
    val start = System.currentTimeMillis()
    val r = sub.next
    val end = System.currentTimeMillis()
    netMillis += (end - start)
    nElems += 1
    r
  }

  def getNetMillis = netMillis
  def getAverageTimePerItem = netMillis / nElems.toDouble

}