第一篇笔记里面,我说groovy运行的居然还满快的,其实是个误会了。我上次做八皇后还是在8080上面用basic做的,和现在奔四上面的groovy相比是没有意义的。特地又做了个对比试验:
1
intq=9
2
int[]i=newint[q]
3
intcount=0
4
longt=System.currentTimeMillis();
5
scan(0)
6
println("totleresults:"+count)
7
println("totletime:"+(System.currentTimeMillis()-t));
8
defscan(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
defcheck(n)
{
22
if(n>0)
23
for(jin0..<n)
24
if(i[j]==i[n]||i[j]-i[n]==j-n||i[j]-i[n]==n-j)
25
returnfalse
26
returntrue
27
}
intq=92
int[]i=newint[q]3
intcount=04
longt=System.currentTimeMillis();5
scan(0)6
println("totleresults:"+count)7
println("totletime:"+(System.currentTimeMillis()-t));8

defscan(n)
{9

if(n==q)
{10
println(i.toList())11
count++12
return13
}14
i[n]=015

while(i[n]<q)
{16
i[n]=i[n]+117
if(check(n))18
scan(n+1)19
}20
}21

defcheck(n)
{22
if(n>0)23
for(jin0..<n)24
if(i[j]==i[n]||i[j]-i[n]==j-n||i[j]-i[n]==n-j)25
returnfalse26
returntrue27
}
运行结果是:totle time:7271 (为了用groovy控制台运行的,直接用groovy命令运行还要慢一点)
java呢?
queens.java:
1
publicclassqueens
{
2
staticintq=9;
3
staticint[]i=newint[q];
4
staticintcount=0;
5
publicstaticvoidmain(String[]args)
{
6
longt=System.currentTimeMillis();
7
scan(0);
8
System.out.println("totleresults:"+count);
9
System.out.println("totletime:"+(System.currentTimeMillis()-t));
10
}
11
privatestaticvoidscan(intn)
{
12
if(n==q)
{
13
for(intk=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
privatestaticbooleancheck(intn)
{
26
for(intj=0;j<n;j++)
{
27
if(i[j]==i[n]||i[j]-i[n]==j-n||i[j]-i[n]==n-j)
{
28
returnfalse;
29
}
30
}
31
returntrue;
32
}
33
}
34

publicclassqueens
{2
staticintq=9;3
staticint[]i=newint[q];4
staticintcount=0;5

publicstaticvoidmain(String[]args)
{6
longt=System.currentTimeMillis();7
scan(0);8
System.out.println("totleresults:"+count);9
System.out.println("totletime:"+(System.currentTimeMillis()-t));10
}11

privatestaticvoidscan(intn)
{12

if(n==q)
{13
for(intk=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

privatestaticbooleancheck(intn)
{26

for(intj=0;j<n;j++)
{27

if(i[j]==i[n]||i[j]-i[n]==j-n||i[j]-i[n]==n-j)
{28
returnfalse;29
}30
}31
returntrue;32
}33
}34
运行结果是:totle time:271
每次运行花费的时间略有不同,groovy和java的运行速度看来大致相差10~30倍左右。
能说这是脚本语言天生的缺陷吗?我们来看看同样是类似java语法的脚本语言javascript在IE里面的速度:
1
varq=9
2
vari=[]
3
varcount=0
4
vard=newDate();
5
scan(0)
6
document.write("totleresults:"+count+"<br>")
7
document.write("timeused:"+(newDate()-d)+"<br>")
8
9
functionscan(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
functioncheck(n)
{
25
for(varj=0;j<n;j++)
26
if(i[j]==i[n]||i[j]-i[n]==j-n||i[j]-i[n]==n-j)
27
returnfalse
28
returntrue
29
}
varq=92
vari=[]3
varcount=04
vard=newDate();5
scan(0)6
document.write("totleresults:"+count+"<br>")7
document.write("timeused:"+(newDate()-d)+"<br>")8

9

functionscan(n)
{10

if(n==q)
{11
document.write(i+"<br>")12
count++13
return14
}15
i[n]=016

while(i[n]<q)
{17
i[n]=i[n]+118

if(check(n))
{19
scan(n+1)20
}21
}22
}23

24

functioncheck(n)
{25
for(varj=0;j<n;j++)26
if(i[j]==i[n]||i[j]-i[n]==j-n||i[j]-i[n]==n-j)27
returnfalse28
returntrue29
}
运行结果是: time used:1241
比groovy快了5倍以上。groovy可真是够慢的。
把groovy编译的class文件反编译了一下,看到groovy生成的代码效率确实是太低了,我们就看循环最内层的check函数吧:
1
defcheck(n)
{
2
if(n>0)
3
for(jin0..<n)
4
if(i[j]==i[n]||i[j]-i[n]==j-n||i[j]-i[n]==n-j)
5
returnfalse
6
returntrue
7
}

defcheck(n)
{2
if(n>0)3
for(jin0..<n)4
if(i[j]==i[n]||i[j]-i[n]==j-n||i[j]-i[n]==n-j)5
returnfalse6
returntrue7
}
编译后变成
1
publicObjectcheck(Objectobj)
2

{
3
if(ScriptBytecodeAdapter.compareGreaterThan(obj,newInteger(0)))
4

{
5
Objectobj1=null;
6
for(Iteratoriterator=ScriptBytecodeAdapter.asIterator(ScriptBytecodeAdapter.createRange(newInteger(0),obj,false));iterator.hasNext();)
7

{
8
Objectobj2=iterator.next();
9
Objectobj3=null;
10
if(ScriptBytecodeAdapter.asBool(ScriptBytecodeAdapter.asBool(ScriptBytecodeAdapter.compareEqual(ScriptBytecodeAdapter.invokeMethod(ScriptBytecodeAdapter.getGroovyObjectProperty(this,"i"),"getAt",((Object)(newObject[]
{
11
obj2
12
}))),ScriptBytecodeAdapter.invokeMethod(ScriptBytecodeAdapter.getGroovyObjectProperty(this,"i"),"getAt",((Object)(newObject[]
{
13
obj
14
}))))||ScriptBytecodeAdapter.compareEqual(ScriptBytecodeAdapter.invokeMethod(ScriptBytecodeAdapter.invokeMethod(ScriptBytecodeAdapter.getGroovyObjectProperty(this,"i"),"getAt",((Object)(newObject[]
{
15
obj2
16
}))),"minus",((Object)(newObject[]
{
17
ScriptBytecodeAdapter.invokeMethod(ScriptBytecodeAdapter.getGroovyObjectProperty(this,"i"),"getAt",((Object)(newObject[]
{
18
obj
19
})))
20
}))),ScriptBytecodeAdapter.invokeMethod(obj2,"minus",((Object)(newObject[]
{
21
obj
22
}))))?((Object)(Boolean.TRUE)):((Object)(Boolean.FALSE)))||ScriptBytecodeAdapter.compareEqual(ScriptBytecodeAdapter.invokeMethod(ScriptBytecodeAdapter.invokeMethod(ScriptBytecodeAdapter.getGroovyObjectProperty(this,"i"),"getAt",((Object)(newObject[]
{
23
obj2
24
}))),"minus",((Object)(newObject[]
{
25
ScriptBytecodeAdapter.invokeMethod(ScriptBytecodeAdapter.getGroovyObjectProperty(this,"i"),"getAt",((Object)(newObject[]
{
26
obj
publicObjectcheck(Objectobj)2


{3
if(ScriptBytecodeAdapter.compareGreaterThan(obj,newInteger(0)))4


{5
Objectobj1=null;6
for(Iteratoriterator=ScriptBytecodeAdapter.asIterator(ScriptBytecodeAdapter.createRange(newInteger(0),obj,false));iterator.hasNext();)7


{8
Objectobj2=iterator.next();9
Objectobj3=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
obj
博客对Groovy、Java和JavaScript的运行速度进行了对比试验。结果显示,Groovy运行速度较慢,与Java相差10 - 30倍左右,而JavaScript比Groovy快5倍以上。反编译Groovy生成的class文件发现其代码效率低。
1452

被折叠的 条评论
为什么被折叠?



