1

我正在实现一个程序,它利用 Groovy 作为脚本引擎,为用户提供了自动化任务的可能性。因此脚本必须按需加载,我使用以下代码实现:

// short version, try/catch and error handling skipped
String[] roots = new String[] { "data" };
Binding binding = new Binding();
GroovyScriptEngine gse = new GroovyScriptEngine(roots);

binding.setVariable("control", this.getControl());
// .. several others

gse.run(scriptName, binding); // where scriptName is provided through user selection

我注意到在第一次运行时整个块的执行需要 400 到 800 毫秒,但在我的笔记本上稍后运行时会下降到 200 到 400 毫秒。

一开始,每个事件都会初始化一个新的 GroovyScriptEngine,然后结束。与此同时,我为整个程序使用了一个 GSE 实例,尽管最初的问题尚未解决:

有没有办法在 GSE 启动时预编译 groovy 脚本?groovyc 不是一个选项,因为这需要“外部”程序调用。

非常感谢!

4

1 回答 1

2

I am looking at the same problem and have the following :

  • Create a single GSE that looks at a shared directory ("data" in your example)
  • The single GSE monitors and pre-compiles the items in the dir so that is the speed up (not sure but the GSE seems to have some staitc stuff that hangs around between instance creation.

If your scripts do not change much then pre-compiling them is the best way to go and when a change is registered re-compile at that time.

Hope that helps.

于 2008-12-12T08:31:47.267 回答