简体   繁体   English

为什么 VSCode 中的代码性能会更好?

[英]Why would code performance be better in VSCode?

I use g++ 12.2.0 (MinGW-w64 via MSYS2) and experience odd performance behavior when running the same code from VSCode (with Code Runner extension) and from the command line.我使用 g++ 12.2.0(通过 MSYS2 的 MinGW-w64)并且从 VSCode(带有 Code Runner 扩展)和命令行运行相同的代码时遇到奇怪的性能行为。 For both, the command used is g++ f1.cpp -o f1 && f1 .对于两者,使用的命令是g++ f1.cpp -o f1 && f1

For example purposes, given this code:出于示例目的,给定此代码:

#include <iostream>
#include <chrono>
#include <string.h>

using 
std::chrono::high_resolution_clock,
std::chrono::duration_cast,
std::chrono::nanoseconds;

void timeit(void (*func)(std::string), std::string message){
    auto time_start = high_resolution_clock::now();
    func(message);
    auto time_end = high_resolution_clock::now();
    auto duration = duration_cast<nanoseconds>(time_end - time_start).count();
    std::cout << "Execution Time: " << duration * 1e-6f << " [ms]" << std::endl;
}

void run(std::string message){
    std::cout << message.c_str() << std::endl;
}

int main() {
    for (size_t i = 0; i < 100; i++)
        timeit(&run, std::to_string(i));
}

VSCode Output: VSCode Output:

... ...

96 Execution Time: 0.0025 [ms] 97 Execution Time: 0.0022 [ms] 98 Execution Time: 0.0034 [ms] 99 Execution Time: 0.0029 [ms] 96 执行时间:0.0025 [ms] 97 执行时间:0.0022 [ms] 98 执行时间:0.0034 [ms] 99 执行时间:0.0029 [ms]

Command Line Output:命令行 Output:

... ...

96 Execution Time: 0.5091 [ms] 97 Execution Time: 0.5168 [ms] 98 Execution Time: 2.4943 [ms] 99 Execution Time: 0.7385 [ms] 96 执行时间:0.5091 [ms] 97 执行时间:0.5168 [ms] 98 执行时间:2.4943 [ms] 99 执行时间:0.7385 [ms]

The performance is significantly different as seen above.如上所示,性能明显不同。 It is also consistent on PowerShell and even the VSCode terminal itself.它在 PowerShell 甚至 VSCode 终端本身也是一致的。

I searched Stack Overflow and other sources for the best of my capability and yet left clueless.我尽我所能搜索 Stack Overflow 和其他资源,但一无所获。

Edit 1: I already tried running it with optimizations and up to 1e6 iterations and the issue still persists.编辑 1:我已经尝试通过优化和最多 1e6 次迭代来运行它,但问题仍然存在。

Edit 2: teapot418 's answer was precise.编辑 2: teapot418的回答很准确。 It really was limited by the performance of the terminal and replacing std::endl with \n made a big difference in performance.它确实受到终端性能的限制,用\n替换std::endl在性能上有很大的不同。 Replacing the print operation with a different (and even more computationally heavy) operation showed equal performance.用不同的(甚至计算量更大的)操作替换打印操作显示出相同的性能。 Colonel Thirty Two 's answer raised an important point too which I should have clarified beforehand.三十二上校的回答也提出了一个重点,我应该事先澄清一下。 Issue solved.问题解决了。

You are benchmarking the time of your run function, which is a single std::cout statement.您正在对run时间 function 进行基准测试,这是一个单独的 std::cout 语句。 Measuring I/O times of a single output line is going to have a lot of variance and depend on where stdout is getting routed to.测量单个 output 行的 I/O 时间会有很大的差异,并且取决于 stdout 被路由到哪里。

My psychic powers suggest that VSCode is re-routing stdout to its own output window which has a lot more buffering and performance than the default console window. And I'm guessing your console window is running under some sort of bash/unix emulation on Windows. (Or is this Linux?)我的通灵能力表明 VSCode 正在将 stdout 重新路由到它自己的 output window,它比默认控制台 window 具有更多的缓冲和性能。我猜你的控制台 window 在 88874814 上的某种 bash/unix 仿真下运行.(或者这是 Linux?)

Some things to try:一些尝试:

  • You can try increasing the buffer size of the Windows console (or the console app you are using to host mingw)您可以尝试增加 Windows 控制台(或您用来托管 mingw 的控制台应用程序)的缓冲区大小
  • Redirect to /dev/null or nul: on Windows. (eg f1 > /dev/null or f1 > nul: ).重定向到 Windows 上的 /dev/null 或 nul:(例如f1 > /dev/nullf1 > nul: )。 Of course, you won't see the output, but you could change your Execution Time statement to be sent to cerr while cout is still used in the run function. But then again, I'm not sure what you are measuring.当然,您不会看到 output,但您可以更改要发送到cerrExecution Time语句,而cout仍用于运行 function。但是话又说回来,我不确定您在测量什么。

  • Try compiling with -O2 optimizations.尝试使用 -O2 优化进行编译。 That might nullify the difference.这可能会消除差异。

  • Try running your compiled program in a default console window instead of the Bash/Mingw/Cygin thing it might be running in now.尝试在默认控制台 window 中运行已编译的程序,而不是它现在可能正在运行的 Bash/Mingw/Cygin 东西。 You could try running it in powershell too.您也可以尝试在 powershell 中运行它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的分散代码的性能优于Vc SIMD? - why the performance of my scatter code better than Vc SIMD? OpenCL 内核性能很差。 为什么我的代码没有 OpenCL 会更好? - OpenCL Kernel performance is very bad. Why my code is better without OpenCL? 考虑干净代码的性能,哪个更好 - Performance with clean code in mind, what is better 为什么并行化会如此显着地降低性能? - Why would parallelization decrease performance so dramatically? 我如何使用大小的运算符删除/删除[],为什么它们更好? - How would I use the sized operators delete/delete[] and why are they better? 为什么OpenMP“ simd”比“ parallel for simd”具有更好的性能? - Why OpenMP 'simd' has better performance than 'parallel for simd'? 为什么使用&#39;if&#39;语句会给我更好的表现? - Why does using an 'if' statement give me better performance? 为什么运行程序的性能会随着时间的推移而变得更好? - Why is the performance of a running program getting better over time? 在合适的情况下编写索引会比 C++ 中的 for 循环性能更好吗? - Would writing indexes in a suitable situation be better than a for-loop performance-wise in c++? 我想使用AVX改善此代码的性能 - I would like to improve the performance of this code using AVX
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM