小编nem*_*ius的帖子

为什么独立代码块的执行时间取决于Scala中的执行顺序?

我有一个用Scala编写的程序.我想测量不同独立代码块的执行时间.当我以明显的方式(即System.nanoTime()在每个块之前和之后插入)执行它时,我观察到执行时间取决于块的顺序.前几个块总是花费比其他块更多的时间.

我创建了一个简单的例子来重现这种行为.hashCode()为简单起见,所有代码块都是相同的并且调用整数数组.

package experiments

import scala.util.Random

/**
  * Measuring execution time of a code block
  *
  * Minimalistic example
  */
object CodeBlockMeasurement {

  def main(args: Array[String]): Unit = {
    val numRecords = args(0).toInt
    // number of independent measurements
    val iterations = args(1).toInt

    // Changes results a little bit, but not too much
    // val records2 = Array.fill[Int](1)(0)
    // records2.foreach(x => {})

    for (_ <- 1 to iterations) {
      measure(numRecords)
    }
  }

  def measure(numRecords: Int): Unit = …
Run Code Online (Sandbox Code Playgroud)

jvm scala measurement

4
推荐指数
2
解决办法
143
查看次数

标签 统计

jvm ×1

measurement ×1

scala ×1