0

I am running a lot of C++ and C code in VSCode, and I have noticed that while my compilation is relatively fast (g++ test.cpp), when I execute the code (./a.exe) there is about a 3 second gap between pressing enter and the code executing.

Here is an example program, that took almost 3 seconds to run:

#include <iostream>
int main(){

    for(int i = 0; i< 50; i++){
        std::cout << i << std::endl;
    }

    return 0;
}

I have noticed this problem on pretty much all my C++/C programs. To reiterate, I believe there is a 3 second gap between hitting enter and actual execution of the program.

I have tried a lot of different things, I have cleared RAM cache, I have deleted temporary files, restarted countless times, but I am unable to find the problem. Additionally, I have noticed that my computer's battery has started running out pretty quickly, but I am not sure if that is related.

If anyone has any tips or has experienced this problem, please let me know so I can fix this really annoying issue.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
cjmess01
  • 1
  • 1
  • 3
    You only have C++ code. – Fiddling Bits Aug 29 '22 at 18:40
  • 1
    One small improvement is to use `'\n'` instead of `std::endl` since the latter does a flush each time. – 001 Aug 29 '22 at 18:43
  • I've had some trouble with my anti-virus when compiling C++ programs, but never with such a simple example. Still might be worth trying temporarily disabling your anti-virus – Marko Borković Aug 29 '22 at 18:43
  • 2
    `std::endl` flush the output. Try using `"\n"` instead. You can flush the buffer at the end of the loop. Besides, the DOS-compatible Windows terminal is insanely slow to print lines and sync data so this is why flushes are so expensive. Linux terminal are far faster (something like 1000 times). Funny point: Linux terminal in WSL are also very fast. Finally, do not forget to enable optimizations with `g++ -O3 -DNDEBUG test.cpp`. – Jérôme Richard Aug 29 '22 at 18:44
  • what build configuration are you using? make sure you are building for release mode and not debug mode. also take a look at [`std::ios_base::sync_with_stdio`](https://en.cppreference.com/w/cpp/io/ios_base/sync_with_stdio). – starball Aug 29 '22 at 18:45
  • 2
    There is no way this program should take 3 seconds. Perhaps it's because it's loading all the MinGW libraries? – user253751 Aug 29 '22 at 18:49
  • @user253751 No, I used (and still use) MinGW many times, all the programs are as fast as expected. I suspect some strange effect of using VSC. – the busybee Aug 29 '22 at 19:13
  • I suppose that is also possible - maybe VSC takes some time – user253751 Aug 29 '22 at 19:14
  • 1
    Between what events do you measure the 3 seconds? Does the first line "0" appear 3 seconds before the last line "49", really? – the busybee Aug 29 '22 at 19:15
  • The loading time may be due to an Anti-Virus program or something that scans your program before it is executed. – Thomas Matthews Aug 29 '22 at 20:21

0 Answers0