---
title: "Groovy 1.6.0 BETA 1 发布了！性能有显著提升！"
author: "seanwon"
date: "2009-02-04T10:21:57Z"
tags: []
slug: "groovy-1-6-0-beta-1"
draft: false
lastmod: "2009-02-04T10:21:57Z"
id: 322712
url: "https://seanwon.iteye.com/blog/322712"
---

转载自：蛟龍居 山不在高，有仙則名；水不在深，有龍則靈 
<a href="http://www.blogjava.net/BlueSUN/archive/2008/05/03/197863.html">链接</a>
**文章中提到的性能提高，本人做过实验，的确提高很多。**

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
 = listOfN(1) 
assert a == 1
assert b == null

 = listOfN(10) 
assert a == 1
assert b == 2

 = 
assert a == 2
assert b == 1

还支持Annotations的定义，（在Groovy 1.6.0之前，Annotations的定义只能放在Java代码中）：
@interface Cachable {
    String cache()
}
最后一个值得关注的新特性就是引入@Bindable这个Annotation
具体使用方法请参考： Groovy高效编程——@Bindable的使用


八皇后问题代码：
*q = 8
i = new int
count = 0

def scan(n){
    if (n == q){
        println(i.toList())
        count++
        return
    }
    i=0
    while (i < q){
        i = i+1
        if (check(n))
            scan(n + 1)
    }
}
def check(n){
    if (n > 0)
        for (j in 0..<n)
            if (i == i || i - i == j - n || i - i == 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)*