在 idea 中执行与在命令行执行,速度怎么相差这么大

92 天前
 chaoschick
public class Main {
public static void main(String[] args) {
long startTime = System.nanoTime();
long startTime2 = System.currentTimeMillis();
for (int i = -1; ++i < 1000; ) {
String row = "Column 1: " + "1" + ", Column 2: " + "2" +
", Column 3: " + "3" + ", Column 3: " + "4";
System.out.println(row);
}
long endTime = System.nanoTime();
long endTime2 = System.currentTimeMillis();

long durationInMillis = (endTime - startTime) / 1_000_000;
long durationInMillis2 = (endTime2 - startTime2) / 1;
System.out.println("Time taken to output the result: " + durationInMillis + " milliseconds");
System.out.println("Time taken to output the result: " + durationInMillis2 + " milliseconds");
}
}


idea
点击运行

Time taken to output the result: 6 milliseconds
Time taken to output the result: 6 milliseconds


cmd 命令提示符
java Main.java

Time taken to output the result: 143 milliseconds
Time taken to output the result: 145 milliseconds
2683 次点击
所在节点    程序员
22 条回复
chaoschick
90 天前
@seanzxx 那多半就是 windows 的终端有问题了 我试过 windows 的 cmd 与 powershell 都有这个问题
chaoschick
90 天前
g++ main.cpp -O3
a.exe

windows 7 (vmware)
Time taken to output the result: 122.007 milliseconds

windows 10 (物理机)
Time taken to output the result: 3.7429 milliseconds

windows server 2019 (内网服务器)
Time taken to output the result: 73.7804 milliseconds

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1067425

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX