6

Why is it faster to call external scala compiler than use the runtime interpreter library? In the code below it takes almost 2s to warm up the interpreter.

val out = new PrintStream(new FileOutputStream("/dev/null"))
val flusher = new java.io.PrintWriter(out)
val interpret = {
   val settings = new scala.tools.nsc.GenericRunnerSettings(println _)
   settings.usejavacp.value = true
   new scala.tools.nsc.interpreter.IMain(settings, flusher)
}
interpret.interpret(" ") //   <-- warming up
interpret.interpret(" Hello World ")

In the other hand, when running Scala compiler from command line like in a shell session:

scala HelloWorld.scala

it takes less than 0.5s to print a Hello World.

I am trying to parse+execute some Java, Scala or similar code given in a string during runtime (it is a script interpreter, i.e. it will be run only one time during my app execution). Scala code would be better obviously, but only if it can be as fast as the Java option. Is there any faster alternative than nsc.interpreter and external compiler to execute code from a string at runtime? The best I could found was Janino; it is faster than Scala compiler and does not require the JDK (a very interesting feature).

As a last resource, how fast are Java Scripting Engines compared to a reflected or bytecode-compiled Java code? I found that, at least, they can be compiled: Compiling oft-used scripts.



Chosen solution: runtimecompilescala.

4

1 回答 1

1

有很多事情没有说明(比如内存设置),但你是在比较苹果和橘子。

命令行脚本运行器不是 REPL 会话;相反,它使用 main 方法将您的代码包装在一个简单的对象中,然后编译并运行它。

相比之下,REPL 中的每个解释行(或可编译的东西)都包装在一个对象中(导入了会话历史记录,以便您可以参考过去的结果)。

即使是模 REPL 启动,这也会对性能产生影响,请参阅此问题

脚本运行器的简单 wrap-it 逻辑内置于解析器中。 以下是脚本运行程序运行编译的方式。或者,看起来这就是 -e 的处理方式。

编辑:您对问题的评论意味着您确实想要 fsc 编译服务器行为。启动 fsc 并使用编译客户端

于 2013-07-01T04:39:22.787 回答