我编写了一个简单的阶乘程序,具有任意精度:
public class Fac {
public static void main(String[] args) {
int stop = 100000;
long start = System.currentTimeMillis();
BigInteger integer = new BigInteger("1");
for(int i = 2; i <= stop; i++){
integer = integer.multiply(new BigInteger(i +""));
}
System.out.println("It took: " + (System.currentTimeMillis() - start) + "ms");
//System.out.println(integer);
}
}
Run Code Online (Sandbox Code Playgroud)
当我在 IntelliJ 中运行它时:
It took: 5392ms
Run Code Online (Sandbox Code Playgroud)
当我在命令行中运行它时:
It took: 17919ms
Run Code Online (Sandbox Code Playgroud)
命令行由以下人员运行:
javac Fac.java
java Fac
Run Code Online (Sandbox Code Playgroud)
我知道这不是衡量时间的最佳方式,但差距太大了,这无关紧要。为什么表现如此不同?
其他人也注意到了类似的差异,但是,据我所知,他们的结论似乎与我的情况无关。