127

分析 Scala 方法调用的标准方法是什么?

我需要的是一个方法的钩子,我可以用它来启动和停止计时器。

在 Java 中,我使用方面编程 aspectJ 来定义要分析的方法并注入字节码来实现相同的目的。

在 Scala 中是否有更自然的方法,我可以在其中定义一堆函数,在函数之前和之后调用,而不会丢失过程中的任何静态类型?

4

13 回答 13

229

您想在不更改要测量时间的代码的情况下执行此操作吗?如果您不介意更改代码,那么您可以执行以下操作:

def time[R](block: => R): R = {
    val t0 = System.nanoTime()
    val result = block    // call-by-name
    val t1 = System.nanoTime()
    println("Elapsed time: " + (t1 - t0) + "ns")
    result
}

// Now wrap your method calls, for example change this...
val result = 1 to 1000 sum

// ... into this
val result = time { 1 to 1000 sum }
于 2012-02-06T12:24:22.913 回答
34

除了 Jesper 的回答,您还可以在 REPL 中自动包装方法调用:

scala> def time[R](block: => R): R = {
   | val t0 = System.nanoTime()
   | val result = block
   | println("Elapsed time: " + (System.nanoTime - t0) + "ns")
   | result
   | }
time: [R](block: => R)R

现在 - 让我们把任何东西都包起来

scala> :wrap time
wrap: no such command.  Type :help for help.

好的 - 我们需要处于电源模式

scala> :power
** Power User mode enabled - BEEP BOOP SPIZ **
** :phase has been set to 'typer'.          **
** scala.tools.nsc._ has been imported      **
** global._ and definitions._ also imported **
** Try  :help,  vals.<tab>,  power.<tab>    **

包起来

scala> :wrap time
Set wrapper to 'time'

scala> BigDecimal("1.456")
Elapsed time: 950874ns
Elapsed time: 870589ns
Elapsed time: 902654ns
Elapsed time: 898372ns
Elapsed time: 1690250ns
res0: scala.math.BigDecimal = 1.456

我不知道为什么打印了 5 次

从 2.12.2 开始更新:

scala> :pa
// Entering paste mode (ctrl-D to finish)

package wrappers { object wrap { def apply[A](a: => A): A = { println("running...") ; a } }}

// Exiting paste mode, now interpreting.


scala> $intp.setExecutionWrapper("wrappers.wrap")

scala> 42
running...
res2: Int = 42
于 2012-02-06T12:47:00.317 回答
25

您可以使用Scala的三个基准测试库。

由于链接站点上的 URL 可能会更改,因此我将相关内容粘贴在下面。

  1. SPerformance - 性能测试框架,旨在自动比较性能测试并在 Simple Build Tool 中工作。

  2. scala-benchmarking-template - SBT 模板项目,用于基于 Caliper 创建 Scala(微)基准。

  3. 指标- 捕获 JVM 和应用程序级别的指标。所以你知道发生了什么

于 2012-02-06T20:17:56.597 回答
25

这是我使用的:

import System.nanoTime
def profile[R](code: => R, t: Long = nanoTime) = (code, nanoTime - t)

// usage:
val (result, time) = profile { 
  /* block of code to be profiled*/ 
}

val (result2, time2) = profile methodToBeProfiled(foo)
于 2013-01-29T01:18:11.503 回答
6

testing.Benchmark可能有用。

scala> def testMethod {Thread.sleep(100)}
testMethod: Unit

scala> object Test extends testing.Benchmark {
     |   def run = testMethod
     | }
defined module Test

scala> Test.main(Array("5"))
$line16.$read$$iw$$iw$Test$     100     100     100     100     100
于 2012-02-06T13:24:25.247 回答
5

我从 Jesper 那里得到了解决方案,并在多次运行相同的代码时添加了一些聚合

def time[R](block: => R) = {
    def print_result(s: String, ns: Long) = {
      val formatter = java.text.NumberFormat.getIntegerInstance
      println("%-16s".format(s) + formatter.format(ns) + " ns")
    }

    var t0 = System.nanoTime()
    var result = block    // call-by-name
    var t1 = System.nanoTime()

    print_result("First Run", (t1 - t0))

    var lst = for (i <- 1 to 10) yield {
      t0 = System.nanoTime()
      result = block    // call-by-name
      t1 = System.nanoTime()
      print_result("Run #" + i, (t1 - t0))
      (t1 - t0).toLong
    }

    print_result("Max", lst.max)
    print_result("Min", lst.min)
    print_result("Avg", (lst.sum / lst.length))
}

假设你要对两个函数counter_new和进行计时counter_old,以下是用法:

scala> time {counter_new(lst)}
First Run       2,963,261,456 ns
Run #1          1,486,928,576 ns
Run #2          1,321,499,030 ns
Run #3          1,461,277,950 ns
Run #4          1,299,298,316 ns
Run #5          1,459,163,587 ns
Run #6          1,318,305,378 ns
Run #7          1,473,063,405 ns
Run #8          1,482,330,042 ns
Run #9          1,318,320,459 ns
Run #10         1,453,722,468 ns
Max             1,486,928,576 ns
Min             1,299,298,316 ns
Avg             1,407,390,921 ns

scala> time {counter_old(lst)}
First Run       444,795,051 ns
Run #1          1,455,528,106 ns
Run #2          586,305,699 ns
Run #3          2,085,802,554 ns
Run #4          579,028,408 ns
Run #5          582,701,806 ns
Run #6          403,933,518 ns
Run #7          562,429,973 ns
Run #8          572,927,876 ns
Run #9          570,280,691 ns
Run #10         580,869,246 ns
Max             2,085,802,554 ns
Min             403,933,518 ns
Avg             797,980,787 ns

希望这会有所帮助

于 2017-08-09T21:30:03.963 回答
4

我使用一种易于在代码块中移动的技术。关键是相同的确切行开始和结束计时器 - 所以它实际上是一个简单的复制和粘贴。另一件好事是你可以将时间对你的意义定义为一个字符串,所有这些都在同一行中。

示例用法:

Timelog("timer name/description")
//code to time
Timelog("timer name/description")

编码:

object Timelog {

  val timers = scala.collection.mutable.Map.empty[String, Long]

  //
  // Usage: call once to start the timer, and once to stop it, using the same timer name parameter
  //
  def timer(timerName:String) = {
    if (timers contains timerName) {
      val output = s"$timerName took ${(System.nanoTime() - timers(timerName)) / 1000 / 1000} milliseconds"
      println(output) // or log, or send off to some performance db for analytics
    }
    else timers(timerName) = System.nanoTime()
  }

优点:

  • 无需将代码包装为块或在行内操作
  • 在探索时可以轻松地在代码行之间移动计时器的开始和结束

缺点:

  • 完全功能性代码的光泽度较低
  • 显然,如果您不“关闭”计时器,则此对象会泄漏映射条目,例如,如果您的代码没有到达给定计时器启动的第二次调用。
于 2014-09-18T23:28:38.140 回答
4

ScalaMeter是一个在 Scala 中执行基准测试的好库

下面是一个简单的例子

import org.scalameter._

def sumSegment(i: Long, j: Long): Long = (i to j) sum

val (a, b) = (1, 1000000000)

val execution_time = measure { sumSegment(a, b) }

如果您在 Scala Worksheet 中执行上述代码片段,您将获得以毫秒为单位的运行时间

execution_time: org.scalameter.Quantity[Double] = 0.260325 ms
于 2016-12-08T23:16:44.083 回答
4

推荐的 Scala 代码基准测试方法是通过sbt-jmh

“不要相信任何人,把一切都摆平。” - JMH (Java Microbenchmark Harness) 的 sbt 插件

许多主要的 Scala 项目都采用这种方法,例如,

基于的简单包装计时器System.nanoTime不是可靠的基准测试方法:

System.nanoTime和现在一样糟糕String.intern:你可以使用它,但要明智地使用它。如果在没有适当严格的情况下完成,计时器引入的延迟、粒度和可扩展性影响可能并且将会影响您的测量。System.nanoTime这是为什么应该通过基准测试框架从用户中抽象出来的众多原因之一

此外,诸如JIT 预热、垃圾收集、系统范围的事件等考虑因素可能会在测量中引入不可预测性

需要减轻大量影响,包括预热、死代码消除、分叉等。幸运的是,JMH 已经处理了很多事情,并且对 Java 和 Scala 都有绑定。

根据Travis Brown 的回答,这里是如何为 Scala 设置 JMH 基准的示例

  1. 将 jmh 添加到project/plugins.sbt
    addSbtPlugin("pl.project13.scala" % "sbt-jmh" % "0.3.7")
    
  2. 启用 jmh 插件build.sbt
    enablePlugins(JmhPlugin)
    
  3. 添加src/main/scala/bench/VectorAppendVsListPreppendAndReverse.scala

    package bench
    
    import org.openjdk.jmh.annotations._
    
    @State(Scope.Benchmark)
    @BenchmarkMode(Array(Mode.AverageTime))
    class VectorAppendVsListPreppendAndReverse {
      val size = 1_000_000
      val input = 1 to size
    
      @Benchmark def vectorAppend: Vector[Int] = 
        input.foldLeft(Vector.empty[Int])({ case (acc, next) => acc.appended(next)})
    
      @Benchmark def listPrependAndReverse: List[Int] = 
        input.foldLeft(List.empty[Int])({ case (acc, next) => acc.prepended(next)}).reverse
    }
    
  4. 执行基准测试
    sbt "jmh:run -i 10 -wi 10 -f 2 -t 1 bench.VectorAppendVsListPreppendAndReverse"
    

结果是

Benchmark                                                   Mode  Cnt  Score   Error  Units
VectorAppendVsListPreppendAndReverse.listPrependAndReverse  avgt   20  0.024 ± 0.001   s/op
VectorAppendVsListPreppendAndReverse.vectorAppend           avgt   20  0.130 ± 0.003   s/op

这似乎表明附加到 aList然后在最后反转它比继续附加到 a 快一个数量级Vector

于 2020-04-04T23:10:17.310 回答
3

我喜欢@wrick 的简单回答,但也想要:

  • 探查器处理循环(为了一致性和方便)

  • 更准确的计时(使用 nanoTime)

  • 每次迭代的时间(不是所有迭代的总时间)

  • 只返回 ns/iteration - 不是元组

这是在这里实现的:

def profile[R] (repeat :Int)(code: => R, t: Long = System.nanoTime) = { 
  (1 to repeat).foreach(i => code)
  (System.nanoTime - t)/repeat
}

为了更准确,一个简单的修改允许一个 JVM Hotspot 预热循环(不定时)来定时小片段:

def profile[R] (repeat :Int)(code: => R) = {  
  (1 to 10000).foreach(i => code)   // warmup
  val start = System.nanoTime
  (1 to repeat).foreach(i => code)
  (System.nanoTime - start)/repeat
}
于 2015-03-05T02:51:51.640 回答
1

站在巨人的肩膀上……

一个可靠的 3rd-party 库会更理想,但如果您需要一些快速和基于标准库的东西,以下变体提供:

  • 重复
  • 多次重复的最后结果获胜
  • 多次重复的总时间和平均时间
  • 无需将时间/即时提供程序作为参数

.

import scala.concurrent.duration._
import scala.language.{postfixOps, implicitConversions}

package object profile {

  def profile[R](code: => R): R = profileR(1)(code)

  def profileR[R](repeat: Int)(code: => R): R = {
    require(repeat > 0, "Profile: at least 1 repetition required")

    val start = Deadline.now

    val result = (1 until repeat).foldLeft(code) { (_: R, _: Int) => code }

    val end = Deadline.now

    val elapsed = ((end - start) / repeat)

    if (repeat > 1) {
      println(s"Elapsed time: $elapsed averaged over $repeat repetitions; Total elapsed time")

      val totalElapsed = (end - start)

      println(s"Total elapsed time: $totalElapsed")
    }
    else println(s"Elapsed time: $elapsed")

    result
  }
}

另外值得注意的是,您可以使用该Duration.toCoarsest方法转换为可能的最大时间单位,尽管我不确定这与运行之间的微小时间差异有多友好,例如

Welcome to Scala version 2.11.7 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_60).
Type in expressions to have them evaluated.
Type :help for more information.

scala> import scala.concurrent.duration._
import scala.concurrent.duration._

scala> import scala.language.{postfixOps, implicitConversions}
import scala.language.{postfixOps, implicitConversions}

scala> 1000.millis
res0: scala.concurrent.duration.FiniteDuration = 1000 milliseconds

scala> 1000.millis.toCoarsest
res1: scala.concurrent.duration.Duration = 1 second

scala> 1001.millis.toCoarsest
res2: scala.concurrent.duration.Duration = 1001 milliseconds

scala> 
于 2016-02-17T11:37:10.487 回答
1

您可以使用System.currentTimeMillis

def time[R](block: => R): R = {
    val t0 = System.currentTimeMillis()
    val result = block    // call-by-name
    val t1 = System.currentTimeMillis()
    println("Elapsed time: " + (t1 - t0) + "ms")
    result
}

用法:

time{
    //execute somethings here, like methods, or some codes.
}  

nanoTime 会告诉你ns,所以很难看到。所以我建议你可以使用 currentTimeMillis 来代替它。

于 2016-09-27T08:41:59.593 回答
0

添加带有名称和秒数的 => 方法

profile[R](block: => R,methodName : String): R = {
    val n = System.nanoTime()
    val result = block
    val n1 = System.nanoTime()
    println(s"Elapsed time: ${TimeUnit.MILLISECONDS.convert(n1 - n,TimeUnit.NANOSECONDS)}ms - MethodName: ${methodName}")
    result
  }
于 2021-05-18T13:10:21.540 回答