其实用阶乘来衡量语言的性能并不是很科学,不过还是能说明一些问题的。
# Factorial.py
def timer(func, n, comment=""):
from time import clock
start = clock()
func(n)
print "consumes %.2f seconds" % (clock()-start)
def factorial(x) :
result = 1
for i in range(1, x+1) :
result *= i
return result
if __name__ == "__main__" :
timer(factorial, 20000, "Calculation of 10000!")
// Factorial.java
import java.math.BigInteger;
public class Factorial {
public static void main(String[] args) {
BigInteger result = new BigInteger("1");
long t1 = System.currentTimeMillis();
for ( int i = 1; i < 10000; i ++)
result = result.multiply(new BigInteger("" + i));
System.out.println(System.currentTimeMillis() - t1) ;
//System.out.println(result);
}
}