java - 如何提高 Groovy 的性能?

标签 java groovy

我正在使用 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/

相关文章:

unit-testing - JSON渲染对于ControllerUnitTestCase中的域对象失败

intellij-idea - Jenkinsfile DSL 源代码

Java 可调用的通用异常

java - 忽略大小写正则表达式

java - 使用 Java 从特定 URL 获取整个网页

java - Wild FJ 论文中类型变量和类型参数的区别?

performance - 在 Grails 和 Groovy 中,字符串(单引号)是否优于 GStrings?

java - 当我使用 Java 在 Android 上生成正弦波时噪音很大

groovy - @CompileStatic 给出 NullPointerException

jenkins - 在 Jenkins 2 管道中执行 SonarQube 扫描仪