概述
第一篇笔记里面,我说groovy运行的居然还满快的,其实是个误会了。我上次做八皇后还是在8080上面用basic做的,和现在奔四上面的groovy相比是没有意义的。特地又做了个对比试验:
1
intq=92int[] i=newint[q]3intcount=04longt=System.currentTimeMillis();5scan(0)6println("totle results:"+count)7println("totle time:"+(System.currentTimeMillis()-t));8
def scan(n){9
if(n==q){10 println(i.toList())11 count++12return13 }14 i[n]=015
while(i[n]{16 i[n]=i[n]+117if(check(n))18 scan(n+1)19 }20}21
def check(n){22if(n>0)23for(jin0..if(i[j]==i[n]||i[j]-i[n]==j-n||i[j]-i[n]==n-j )25returnfalse26returntrue27}
运行结果是:totle time:7271 (为了用groovy控制台运行的,直接用groovy命令运行还要慢一点)
java呢?
queens.java:
1
publicclassqueens{2staticintq=9;3staticint[] i=newint[q];4staticintcount=0;5
publicstaticvoidmain(String[] args){6longt=System.currentTimeMillis();7 scan(0);8 System.out.println("totle results:"+count);9 System.out.println("totle time:"+(System.currentTimeMillis()-t));10 }11
privatestaticvoidscan(intn){12
if(n==q){13for(intk=0;k count++;15return;16 }17 i[n]=0;18
while(i[n]{19 i[n]=i[n]+1;20
if(check(n)){21 scan(n+1);22 }23 }24 }25
privatestaticboolean check(intn){26
for(intj=0;j{27
if(i[j]==i[n]||i[j]-i[n]==j-n||i[j]-i[n]==n-j ){28returnfalse;29 }30 }31returntrue;32 }33}34
运行结果是:totle time:271
每次运行花费的时间略有不同,groovy和java的运行速度看来大致相差10~30倍左右。
能说这是脚本语言天生的缺陷吗?我们来看看同样是类似java语法的脚本语言javascript在IE里面的速度:
1
var q=92var i=[]3var count=04var d=newDate();5scan(0)6document.write("totle results:"+count+"
")7document.write("time used:"+(newDate()-d)+"
")89
function scan(n){10
if(n==q){11 document.write(i+"
")12 count++13return14 }15 i[n]=016
while(i[n]{17 i[n]=i[n]+118
if(check(n)){19 scan(n+1)20 }21 }22}2324
function check(n){25for(var j=0; jif(i[j]==i[n]||i[j]-i[n]==j-n||i[j]-i[n]==n-j )27returnfalse28returntrue29}
运行结果是: time used:1241
比groovy快了5倍以上。groovy可真是够慢的。
把groovy编译的class文件反编译了一下,看到groovy生成的代码效率确实是太低了,我们就看循环最内层的check函数吧:
1
def check(n){2if(n>0)3for(jin0..if(i[j]==i[n]||i[j]-i[n]==j-n||i[j]-i[n]==n-j )5returnfalse6returntrue7}
编译后变成
1
publicObject check(Object obj)2
{3if(ScriptBytecodeAdapter.compareGreaterThan(obj,newInteger(0)))4
{5 Object obj1=null;6for(Iterator iterator=ScriptBytecodeAdapter.asIterator(ScriptBytecodeAdapter.createRange(newInteger(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) (newObject[]{11 obj212
}))), ScriptBytecodeAdapter.invokeMethod(ScriptBytecodeAdapter.getGroovyObjectProperty(this,"i"),"getAt", ((Object) (newObject[]{13 obj14
}))))||ScriptBytecodeAdapter.compareEqual(ScriptBytecodeAdapter.invokeMethod(ScriptBytecodeAdapter.invokeMethod(ScriptBytecodeAdapter.getGroovyObjectProperty(this,"i"),"getAt", ((Object) (newObject[]{15 obj216
}))),"minus", ((Object) (newObject[]{17
ScriptBytecodeAdapter.invokeMethod(ScriptBytecodeAdapter.getGroovyObjectProperty(this,"i"),"getAt", ((Object) (newObject[]{18 obj19 })))20
}))), ScriptBytecodeAdapter.invokeMethod(obj2,"minus", ((Object) (newObject[]{21 obj22
}))))?((Object) (Boolean.TRUE)) : ((Object) (Boolean.FALSE)))||ScriptBytecodeAdapter.compareEqual(ScriptBytecodeAdapter.invokeMethod(ScriptBytecodeAdapter.invokeMethod(ScriptBytecodeAdapter.getGroovyObjectProperty(this,"i"),"getAt", ((Object) (newObject[]{23 obj224
}))),"minus", ((Object) (newObject[]{25
ScriptBytecodeAdapter.invokeMethod(ScriptBytecodeAdapter.getGroovyObjectProperty(this,"i"),"getAt", ((Object) (newObject[]{26 obj27 })))28
}))), ScriptBytecodeAdapter.invokeMethod(obj,"minus", ((Object) (newObject[]{29 obj230}))))?((Object) (Boolean.TRUE)) : ((Object) (Boolean.FALSE))))31returnBoolean.FALSE;32 }3334 }35returnBoolean.TRUE;36 }37
一切都是object,做任何事情都是invokeMethod,两个整数的比较居然要写将近400个字符的代码,光看代码量都可以吓倒我了。这是我们期待的脚本语言吗?
groovy可以嵌入到java代码里面,但是java代码可以嵌入到groovy里面吗?我觉得groovy有必要提供这样一种机制,在有必要的时候可以消除性能瓶颈。可是现在只看到groovy里面可以通过Scriptom(现在还是beta版)嵌入vbs、js脚本(包括使用WSH,FSO)或者调用InternetExplorer、Media Player、Word和Excel等windows组件。看来对消除性能瓶颈的帮助不大。
最后
以上就是无语墨镜为你收集整理的groovy 慢_Groovy 学习笔记3 运行效率的全部内容,希望文章能够帮你解决groovy 慢_Groovy 学习笔记3 运行效率所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复