我正在使用 Groovy 执行一些 Java 代码。 就我的目的而言,Groovy 很容易使用,因为我必须执行的代码具有我无法预测的任意数量的参数,因为它取决于用户输入。 我所说的输入是 OWL 公理,它们是嵌套的。 这是我的代码:
//The reflection
static void reflectionToOwl() {
Binding binding = new Binding(); //155 ms
GroovyShell shell = new GroovyShell(binding);
while (!OWLMapping.axiomStack.isEmpty()) {
String s = OWLMapping.axiomStack.pop();
shell.evaluate(s); //350 ms
}
}
我的程序中唯一的瓶颈就在这里。更多的是我必须处理的数据,更多的是我必须等待的毫秒。 您有什么建议吗?
最佳答案
如果需要提高 Groovy 性能,可以使用 @CompileStatic
注释。
This will let the Groovy compiler use compile time checks in the style of Java then perform static compilation, thus bypassing the Groovy meta object protocol.
只需用它注释具体的方法即可。但请确保您不在该范围内使用任何动态功能。
举个例子:
import groovy.transform.CompileStatic
@CompileStatic
class Static {
}
class Dynamic {
}
println Static.declaredMethods.length
Static.declaredMethods.collect { it.name }.each { println it }
println('-' * 100)
println Dynamic.declaredMethods.length
Dynamic.declaredMethods.collect{ it.name }.each { println it }
不会生成一些额外的方法:
6
invokeMethod
getMetaClass
setMetaClass
$getStaticMetaClass
setProperty
getProperty
8
invokeMethod
getMetaClass
setMetaClass
$getStaticMetaClass
$getCallSiteArray
$createCallSiteArray
setProperty
getProperty
关于java - 如何提高 Groovy 的性能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42034016/