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.7Groovy1.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
還支持Annotations的定義,(在Groovy 1.6.0之前,Annotations的定義只能放在Java代碼中):@interface?Cachable?{
????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
posted on 2008-05-03 11:52
山風小子 閱讀(2308)
評論(5) 編輯 收藏 所屬分類:
Groovy & Grails