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!