string interpolation - Scala StringContext, how can I intercept expressions before evaluation? - Stack Overflow

I'm trying to use the Scala extensions to extend the String context to time the execution of a cos

I'm trying to use the Scala extensions to extend the String context to time the execution of a costly method And I came with this:

extension(sc: StringContext)
  def timeit(args: Any*): String = {
    val startTime = System.currentTimeMillis();
    // sc.parts.evaluate??
    val result = sc.s(args*) + s.parts().mkstring("");
    result + " took " + (System.currentTimeMillis() - startTime) + " millisecs"
  }

Unfortunately I don't see a way to intercept before the sc.parts are evaluated, so there is not much sense in measuring time. Is there a way to record the startTime before the sc.parts are evaluated? Do a call by name somehow? Or some other ways?

I'm trying to use the Scala extensions to extend the String context to time the execution of a costly method And I came with this:

extension(sc: StringContext)
  def timeit(args: Any*): String = {
    val startTime = System.currentTimeMillis();
    // sc.parts.evaluate??
    val result = sc.s(args*) + s.parts().mkstring("");
    result + " took " + (System.currentTimeMillis() - startTime) + " millisecs"
  }

Unfortunately I don't see a way to intercept before the sc.parts are evaluated, so there is not much sense in measuring time. Is there a way to record the startTime before the sc.parts are evaluated? Do a call by name somehow? Or some other ways?

Share Improve this question asked Mar 23 at 20:54 dr jerrydr jerry 10k25 gold badges85 silver badges127 bronze badges 1
  • IIRC, you need to use inline and a macro in order to receive the parts as the original expressions rather than as the results. – Luis Miguel Mejía Suárez Commented Mar 23 at 21:21
Add a comment  | 

1 Answer 1

Reset to default 3

You need to use inline def, but you also need to make sure that all arguments are inline as well (so that their code would be inlined, not their result):

// code where all arguments are inlined
extension(inline sc: StringContext)
  inline def timeit(inline args: Any*): String = {
    val startTime = System.currentTimeMillis()
    val result = sc.s(args*)
    val endTime = System.currentTimeMillis()
    s"$result took ${endTime - startTime} millisecs"
  }

println(timeit"This string ${ { Thread.sleep(100); "is" } } taking ${ { Thread.sleep(250); "a while" } }")

// code where arguments are NOT inlined
extension(sc: StringContext)
  inline def timeit2(args: Any*): String = {
    val startTime = System.currentTimeMillis()
    val result = sc.s(args*)
    val endTime = System.currentTimeMillis()
    s"$result took ${endTime - startTime} millisecs"
  }

println(timeit2"This string ${ { Thread.sleep(100); "is" } } taking ${ { Thread.sleep(250); "a while" } }")
This string is taking a while took 354 millisecs
This string is taking a while took 1 millisecs

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744269581a4566034.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信