为什么这段相同逻辑的代码在同一个机子上运行时,java的竟然比lua快那么多?
Java代码:
import java.lang.Math;
import java.util.*;
import java.util.Date;
public class Test {
public int isprime(int num){
int count = (int) Math.sqrt(num);
while (count >1){
if (num % count == 0){
return 0;
}
count -= 1;
}
return 1;
}
public static void main(String[] args) {
Test t = new Test();
Date begin = new Date();
for (int i=0;i<100000;i++){
t.isprime(i);
}
Date end = new Date();
long interval = end.getTime() - begin.getTime();
System.out.println(begin.getTime());
System.out.println(end.getTime());
System.out.println(interval);
}
}
输出:
1516427548266
1516427548329
63
Lua代码:
function isprime(num)
t1,t2 = math.modf(math.sqrt(num));
while (t1 > 1)
do
if (num % t1 == 0) then
return 0;
end
t1 = t1 - 1;
end
return 1;
end
time1 = os.clock();
print(time1);
for i=2,100000,1 do
isprime(i);
end
time2 = os.clock();
print(time2);
print((time2 - time1));
输出:
lua -e "io.stdout:setvbuf 'no'" "test.lua"
0.001
1.464
1.463
Exit code: 0
偶然的你
翻过高山走不出你
慕尼黑8549860
相关分类