0

C++ code compiled from a command line shell script runs much faster than when I use the same shell script from a Task:Run in VSCode. I successfully tried an echo command in the script to make sure VSCode was running the same script I use at the command line.

I know there are compiler settings in a .json file, but I don't see how to add a -O3 optimization switch, and since VSCode is running the same script I used at the command line I just don't get why it might be different. I'm on a new M1 Mac and here's my script: clang++ -std=c++17 -O3 -o $1 $1.cpp && $1

The VSCode resulting speed is midway between the -O3 option and no optimization. Weird.... At first I thought it might be the terminal in VSCode but the code runs same slowness if I execute it at the external command line, so that's not it.

JaMiT
  • 14,422
  • 4
  • 15
  • 31
  • I know it's confusing. I searched quite a bit and can only find things about VSC itself running slowly. Nothing about it affecting compiled code afterward. I'm trying to imagine how executing a command line script from inside VSC can give a different result from the same script at the command line. Clearly VSC is involved somehow. I just can find any ideas how that can be. – Jeff Lastofka Jan 24 '21 at 20:58
  • So it's not a compile time speed issue. It's the speed of the resulting code. The final program runs five times slower. The compile time is just a couple seconds either way. – Jeff Lastofka Jan 24 '21 at 21:18
  • 1
    *"I know it's confusing."* -- by "it" do you mean the situation or your description of it? Personally, I found your description to be a bit rambling, unfocused, and hard to follow. Sort of like you jumped into the middle of your thoughts with no introduction. (Might just be me, though.) – JaMiT Jan 24 '21 at 21:57
  • I thought you were asking about a compile time problem meaning that it took 5 minutes to build your code instead of 3. You probably can enable optimizations by editing your `tasks.json` [https://code.visualstudio.com/docs/cpp/config-clang-mac#_build-helloworldcpp](https://code.visualstudio.com/docs/cpp/config-clang-mac#_build-helloworldcpp) – drescherjm Jan 24 '21 at 22:05
  • After a LOT of experimenting and digging, I have the answer: the VSCode environment makes the clang++ compiler compile to x86_64 code, and I have an M1 Mac. When I compile from the command line, I get arm64 code and it runs 4 times as fast. Even using the terminal window inside VSCode to execute my shell script it still forces it to x86 mode. I haven't found any mention of this anywhere I've been looking, which has been extensive. I discovered it myself this time around. I'll look for a native VSCode, or a setting, or ... Thanks for trying to help, though. – Jeff Lastofka Jan 25 '21 at 02:49

1 Answers1

2

I found the answer. Turns out it's because VSC isn't Apple Silicon native yet and I'm on an M1 Mac. Anything I did from inside VSC, even the terminal window, forced the results to be x86_64 code, which runs 4x slower than the arm64 code I compile from outside VSC. Need to wait for the native version I guess.

  • If you don't need to do integrated debugging -- perhaps it'd be possible to bind the "build" option of VSCode to write to a pipe where the other end builds the code natively – M.M Jan 25 '21 at 03:37
  • If you are satisfied that all of the relevant information from your comment is in this answer, you could delete your comment (to keep this page tidier) then remove the *"I put it in a comment but should have put it here."* bit from your answer. – JaMiT Jan 31 '21 at 02:16