第一篇筆記里面,我說groovy運行的居然還滿快的,其實是個誤會了。我上次做八皇后還是在8080上面用basic做的,和現(xiàn)在奔四上面的groovy相比是沒有意義的。特地又做了個對比試驗: 1 int q=9 2 int[] i=new int[q] 3 int count=0 4 long t = System.currentTimeMillis(); 5 scan(0) 6 println("totle results:"+count) 7 println("totle time:"+(System.currentTimeMillis()-t)); 8 def scan(n) { 9 if (n==q) { 10 println(i.toList()) 11 count++ 12 return 13 } 14 i[n]=0 15 while(i[n]<q) { 16 i[n] = i[n]+1 17 if (check(n)) 18 scan(n+1) 19 } 20 } 21 def check(n) { 22 if (n>0) 23 for (j in 0..<n) 24 if (i[j]==i[n] || i[j]-i[n]==j-n || i[j]-i[n]==n-j ) 25 return false 26 return true 27 } 運行結(jié)果是:totle time:7271 (為了用groovy控制臺運行的,直接用groovy命令運行還要慢一點)
java呢? queens.java: 1 public class queens { 2 static int q=9; 3 static int[] i=new int[q]; 4 static int count=0; 5 public static void main(String[] args) { 6 long t = System.currentTimeMillis(); 7 scan(0); 8 System.out.println("totle results:"+count); 9 System.out.println("totle time:"+(System.currentTimeMillis()-t)); 10 } 11 private static void scan(int n) { 12 if (n==q) { 13 for (int k=0;k<q;k++) System.out.print(i[k]+(k==q-1?"\n":",")); 14 count++; 15 return; 16 } 17 i[n]=0; 18 while(i[n]<q) { 19 i[n] = i[n]+1; 20 if (check(n)) { 21 scan(n+1); 22 } 23 } 24 } 25 private static boolean check(int n) { 26 for(int j=0;j<n;j++) { 27 if (i[j]==i[n] || i[j]-i[n]==j-n || i[j]-i[n]==n-j ) { 28 return false; 29 } 30 } 31 return true; 32 } 33 } 34 運行結(jié)果是:totle time:271
每次運行花費的時間略有不同,groovy和java的運行速度看來大致相差10~30倍左右。
能說這是腳本語言天生的缺陷嗎?我們來看看同樣是類似java語法的腳本語言javascript在IE里面的速度:
1 var q=9 2 var i=[] 3 var count=0 4 var d = new Date(); 5 scan(0) 6 document.write("totle results:"+count+"<br>") 7 document.write("time used:"+(new Date()-d)+"<br>") 8 9 function scan(n) { 10 if (n==q) { 11 document.write(i+"<br>") 12 count++ 13 return 14 } 15 i[n]=0 16 while(i[n]<q) { 17 i[n] = i[n]+1 18 if (check(n)) { 19 scan(n+1) 20 } 21 } 22 } 23 24 function check(n) { 25 for (var j=0; j<n;j++) 26 if (i[j]==i[n] || i[j]-i[n]==j-n || i[j]-i[n]==n-j ) 27 return false 28 return true 29 }
運行結(jié)果是: time used:1241 比groovy快了5倍以上。groovy可真是夠慢的。
把groovy編譯的class文件反編譯了一下,看到groovy生成的代碼效率確實是太低了,我們就看循環(huán)最內(nèi)層的check函數(shù)吧:
1 def check(n) { 2 if (n>0) 3 for (j in 0..<n) 4 if (i[j]==i[n] || i[j]-i[n]==j-n || i[j]-i[n]==n-j ) 5 return false 6 return true 7 }
編譯后變成
1 public Object check(Object obj) 2 { 3 if(ScriptBytecodeAdapter.compareGreaterThan(obj, new Integer(0))) 4 { 5 Object obj1 = null; 6 for(Iterator iterator = ScriptBytecodeAdapter.asIterator(ScriptBytecodeAdapter.createRange(new Integer(0), obj, false)); iterator.hasNext();) 7 { 8 Object obj2 = iterator.next(); 9 Object obj3 = null; 10 if(ScriptBytecodeAdapter.asBool(ScriptBytecodeAdapter.asBool(ScriptBytecodeAdapter.compareEqual(ScriptBytecodeAdapter.invokeMethod(ScriptBytecodeAdapter.getGroovyObjectProperty(this, "i"), "getAt", ((Object) (new Object[] { 11 obj2 12 }))), ScriptBytecodeAdapter.invokeMethod(ScriptBytecodeAdapter.getGroovyObjectProperty(this, "i"), "getAt", ((Object) (new Object[] { 13 obj 14 })))) || ScriptBytecodeAdapter.compareEqual(ScriptBytecodeAdapter.invokeMethod(ScriptBytecodeAdapter.invokeMethod(ScriptBytecodeAdapter.getGroovyObjectProperty(this, "i"), "getAt", ((Object) (new Object[] { 15 obj2 16 }))), "minus", ((Object) (new Object[] { 17 ScriptBytecodeAdapter.invokeMethod(ScriptBytecodeAdapter.getGroovyObjectProperty(this, "i"), "getAt", ((Object) (new Object[] { 18 obj 19 }))) 20 }))), ScriptBytecodeAdapter.invokeMethod(obj2, "minus", ((Object) (new Object[] { 21 obj 22 })))) ? ((Object) (Boolean.TRUE)) : ((Object) (Boolean.FALSE))) || ScriptBytecodeAdapter.compareEqual(ScriptBytecodeAdapter.invokeMethod(ScriptBytecodeAdapter.invokeMethod(ScriptBytecodeAdapter.getGroovyObjectProperty(this, "i"), "getAt", ((Object) (new Object[] { 23 obj2 24 }))), "minus", ((Object) (new Object[] { 25 ScriptBytecodeAdapter.invokeMethod(ScriptBytecodeAdapter.getGroovyObjectProperty(this, "i"), "getAt", ((Object) (new Object[] { 26 obj 27 }))) 28 }))), ScriptBytecodeAdapter.invokeMethod(obj, "minus", ((Object) (new Object[] { 29 obj2 30 })))) ? ((Object) (Boolean.TRUE)) : ((Object) (Boolean.FALSE)))) 31 return Boolean.FALSE; 32 } 33 34 } 35 return Boolean.TRUE; 36 } 37
一切都是object,做任何事情都是invokeMethod,兩個整數(shù)的比較居然要寫將近400個字符的代碼,光看代碼量都可以嚇倒我了。這是我們期待的腳本語言嗎?
groovy可以嵌入到j(luò)ava代碼里面,但是java代碼可以嵌入到groovy里面嗎?我覺得groovy有必要提供這樣一種機制,在有必要的時候可以消除性能瓶頸。可是現(xiàn)在只看到groovy里面可以通過Scriptom(現(xiàn)在還是beta版)嵌入vbs、js腳本(包括使用WSH,FSO)或者調(diào)用InternetExplorer、Media Player、Word和Excel等windows組件。看來對消除性能瓶頸的幫助不大。
|