|  |      1lucasj      2024-08-24 10:43:49 +08:00 可能运行参数不一样。idea 运行加了一堆 jvm 参数 | 
|  |      2chaoschick OP @lucasj 我把控制台中的 最上面那一部分的命令完全 copy 后放到 bat 批处理脚本 然后运行 Time taken to output the result: 121 milliseconds Time taken to output the result: 121 milliseconds | 
|  |      3mightybruce      2024-08-24 11:00:35 +08:00 在控制台运行包含 jit 编译吧, 这个时间这么长 只有编译才需要这么久。 | 
|      4FrankAdler      2024-08-24 12:46:37 +08:00 via Android 你 hold 主进程不退出,看下 idea 实际执行的可能是编译后的 | 
|      5kneo      2024-08-24 13:07:39 +08:00 via Android 命令行先编译再执行。 | 
|  |      6chendy      2024-08-24 13:08:59 +08:00 因为一样是 System.out.println ,打印到不同的地方耗时不一样,至于为什么不一样我就解释不出来了(可以问问 ai ?) 我这里把 1000 改成 10000 idea 执行 82 命令行 1452 命令行重定向到文件 55 不打印到屏幕上还是更快 | 
|  |      7chaoschick OP @kneo 试了 编译后再执行 Time taken to output the result: 122 milliseconds Time taken to output the result: 122 milliseconds | 
|  |      8chaoschick OP 编译 javac Main.java 运行 java Main | 
|  |      9chaoschick OP @chendy 我也怀疑这个 idea 可能做了什么操作 我使用 c++ 写了一个一样逻辑的代码 然后 g++ main.cpp -O3 && a.exe 测试发现也需要 100ms 左右 所以我怀疑 idea 的控制台 | 
|  |      10chendy      2024-08-24 13:24:35 +08:00 @chaoschick 参考这个 https://stackoverflow.com/questions/42405829/performance-using-stdout-screen-vs-regular-file 应该是 buffer 的问题,某些场景 buffer 更大,flush 更少,于是更快 | 
|  |      11chaoschick OP @chendy  #include <iostream> #include <chrono> #include <sstream> int main() { // Output the result auto start_time = std::chrono::high_resolution_clock::now(); std::ios::sync_with_stdio(false); std::cin.tie(NULL); const size_t bufferSize = 1024 * 1024 * 100; char *buffer = new char[bufferSize]; std::cout.rdbuf()->pubsetbuf(buffer, bufferSize); std::ostringstream oss; for (int i = -1; ++i < 1000; ) { oss << "Column 1: 1, Column 2: 2, Column 3: 3, Column 4: 4\n"; } std::cout << oss.str(); auto end_time = std::chrono::high_resolution_clock::now(); std::chrono::duration<double, std::milli> elapsed = end_time - start_time; std::cout << "Time taken to output the result: " << elapsed.count() << " milliseconds\n"; delete[] buffer; return 0; } 编译 g++ main.cpp -O3 运行 a.exe Time taken to output the result: 124.007 milliseconds 这是 c++ 写的代码 我已经将 buffer 调大了 但是还是维持在 100ms 左右 | 
|  |      12leconio      2024-08-24 14:23:41 +08:00 via iPhone idea 不是默认 jbr 吗,你本地啥运行时。 | 
|      13kneo      2024-08-24 14:38:58 +08:00 via Android 你把 bat 全贴一下。 | 
|  |      14chaoschick OP @leconio 我下载了 jbr17 D:\cpp\demo4\jbrsdk-17.0.9-windows-x64-b1087.7\bin\java Main.java Time taken to output the result: 124 milliseconds Time taken to output the result: 124 milliseconds | 
|  |      15chaoschick OP #include <iostream> #include <chrono> #include <sstream> int main() { // Output the result auto start_time = std::chrono::high_resolution_clock::now(); std::ios::sync_with_stdio(false); std::cin.tie(NULL); const size_t bufferSize = 1024 * 1024 * 100; char *buffer = new char[bufferSize]; std::cout.rdbuf()->pubsetbuf(buffer, bufferSize); std::ostringstream oss; for (int i = -1; ++i < 1000; ) { oss << "Column 1: 1, Column 2: 2, Column 3: 3, Column 4: 4\n"; } std::cout << oss.str(); auto end_time = std::chrono::high_resolution_clock::now(); std::chrono::duration<double, std::milli> elapsed = end_time - start_time; std::cout << "Time taken to output the result: " << elapsed.count() << " milliseconds\n"; delete[] buffer; return 0; } 我把同样的代码 在 linux 跑了一遍 g++ main.cpp -O3 ./a.out Time taken to output the result: 11.9875 milliseconds 。。。 | 
|  |      16RecursiveG      2024-08-24 15:22:14 +08:00 你重定向到文件试试 | 
|  |      17chaoschick OP @RecursiveG 很快 我已经试过了 重定向到文件 10ms 以内了 | 
|  |      18RecursiveG      2024-08-24 15:31:14 +08:00 那就是终端的瓶颈了, 我记得 cmd.exe 很慢的,你换个终端再试试 | 
|      20seanzxx      2024-08-25 02:37:28 +08:00 你大量的文字输出,是在测试终端的处理和渲染的性能。 IDEA 的结果输出,只需要输出程序的控制台输出,可以简单的处理成输出到一个 buffer 或者文件,再输出到窗口,速度快是自然的。 终端 (Terminal) 复杂得多,举个简单的例子,它需要处理键盘输入,你还可以用 Ctrl + C 终端程序运行。 IntelliJ 里面带有完整功能 Terminal 界面的就慢很多。 在我的 MacBook 上, 这个代码在相同的 JDK 中耗费的时间是 (我把代码的 for 循环从 1000 改成了 10000 ,不然看不出差距): IDEA 的 run: Time taken to output the result: 25 milliseconds Time taken to output the result: 26 milliseconds IDEA 的 terminal: Time taken to output the result: 188 milliseconds Time taken to output the result: 188 milliseconds MacOS 原生的 Terminal: Time taken to output the result: 32 milliseconds Time taken to output the result: 33 milliseconds kitty(一个第三方 terminal 程序,使用了 GPU 加速,一般认为比系统自带的终端快): Time taken to output the result: 29 milliseconds Time taken to output the result: 30 milliseconds | 
|  |      21chaoschick OP @seanzxx 那多半就是 windows 的终端有问题了 我试过 windows 的 cmd 与 powershell 都有这个问题 | 
|  |      22chaoschick OP 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 |