本文介绍了如何在Scala中剖析方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



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

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

在Scala中有更自然的方式,我可以在函数前后定义一堆函数:

解决方案

您是否想在不更改要测量计时的代码的情况下执行此操作?如果你不介意修改代码,那么你可以这样做:

  def time [R](block: => R):R = {
val t0 = System.nanoTime()
val result = block //调用名称
val t1 = System.nanoTime()
println(Elapsed time:+(t1 - t0)+ns)
结果
}

//现在包装你的方法调用,例如改变它...
val结果= 1至1000总和

// ...进入此
结果=时间{1至1000总和}


What is a standard way of profiling Scala method calls?

What I need are hooks around a method, using which I can use to start and stop Timers.

In Java I use aspect programming, aspectJ, to define the methods to be profiled and inject bytecode to achieve the same.

Is there a more natural way in Scala, where I can define a bunch of functions to be called before and after a function without losing any static typing in the process?

解决方案

Do you want to do this without changing the code that you want to measure timings for? If you don't mind changing the code, then you could do something like this:

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 }

这篇关于如何在Scala中剖析方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 05:05