views:

205

answers:

1

I'm implementing a program which utilizes Groovy as scripting engine offering users the possibility i.e. to automate tasks. Hence scripts have to be loaded on demand, which I achieve with the following code:

// 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

I've noticed that the execution takes between 400 and 800 milliseconds for the whole block on the fist run, but goes down to 200 to 400 on later runs on my notebook.

At the beginning a new GroovyScriptEngine was initialized for each event and ended afterwords. In the meantime I use one GSE instance for the whole program, although the initial question is yet open:

Is there a way to precompile groovy scripts i.e. at the startup of the GSE? groovyc is not an option since that would required an "external" programm call.

Many thanks!

+2  A: 

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.

LenW
I couldn't find a way to precompile the groovy scripts except with groovyc, but the GroovyScriptEngine doesn't pick the proper script if it's only available as *.class (at least not with the code described in the initial question). Is there another way to achieve the same?
MrG