Groovy 1.6.0 BETA 1 发布了!性能有显著提升!
Groovy 1.6.0 BETA 1终于发布了,除了一些BUG修正外,最令人兴奋的是,Groovy的运行效率有了显著的提升。官方用Great Language Shootout的基准测试得出Groovy 1.6.0 BETA 1的性能相比Groovy 1.5.6 GA,提升了150%~460%。
眼见为实,耳听为虚,我自己也做了一个相对简单的性能测试:
利用Groovy 1.6.0 BETA 1将下面解决八皇后问题的代码执行10次,结果如下:
1 2 3 4 5 6 7 8 9 10 AVG
Groovy1.5.6GA : 1360 1156 969 1000 1063 1110 938 1046 1031 954 1062.7
Groovy1.6.0BETA1: 187 171 141 109 187 156 172 141 203 187 165.4
经过计算,Groovy1.6.0BETA1的性能相比Groovy1.5.6GA,提升了542.5%。
如果是普通应用程序代码的话,提升的幅度会小一点。
此外,Groovy1.6.0BETA1还支持Multiple assignments(多重赋值)
def listOfN(numOfElem) {
1 ..numOfElem
}
def a, b
[a, b] = listOfN( 1 )
assert a == 1
assert b == null
[a, b] = listOfN( 10 )
assert a == 1
assert b == 2
[a, b] = [b, a]
assert a == 2
assert b == 1
1 ..numOfElem
}
def a, b
[a, b] = listOfN( 1 )
assert a == 1
assert b == null
[a, b] = listOfN( 10 )
assert a == 1
assert b == 2
[a, b] = [b, a]
assert a == 2
assert b == 1
还支持Annotations的定义,(在Groovy 1.6.0之前,Annotations的定义只能放在Java代码中):
@
interface
Cachable {
String cache()
}
String cache()
}
最后一个值得关注的新特性就是引入@Bindable这个Annotation
具体使用方法请参考: Groovy高效编程——@Bindable的使用
八皇后问题代码:
q = 8
i = new int[q]
count = 0
def scan(n){
if (n == q){
println(i.toList())
count++
return
}
i[n]=0
while (i[n] < q){
i[n] = i[n]+1
if (check(n))
scan(n + 1)
}
}
def check(n){
if (n > 0)
for (j in 0..<n)
if (i[j] == i[n] || i[j] - i[n] == j - n || i[j] - i[n] == n - j)
return false
return true
}
long t1 = System.currentTimeMillis()
scan(0)
long t2 = System.currentTimeMillis()
println("total time:" + ( t2 - t1)) // 耗时
println("total results:" + count)
下载地址:http://dist.groovy.codehaus.org/distributions/groovy-binary-1.6-beta-1.zip
附: 朝花夕拾——Groovy & Grails