2022-07-30

Why do g++ binaries have a significantly slower startup time than clang++?

Whenever I compile code using g++, I've noticed that when I run the binary repeatedly, it runs significantly slower than when I compile with clang++.

test.cpp

#include <iostream>

int main() {
    std::cout << "Hello World!" << '\n';
}

script.sh

for ((i=0; i<2000; ++i)); do
    ./a.out
done

Compiling using g++:
g++-11 -O2 test.cpp
time bash script.sh -> 10.32s user 5.37s system 88% cpu 17.751 total

Compiling using clang++:
clang++ -O2 test.cpp
time bash script.sh -> 1.42s user 1.50s system 69% cpu 4.223 total


This is extremely annoying as I need to use g++, but I also need to speed up the binary to make it easier to stress test my code, so an explanation or fix would be welcome.

Note: GCC (11.3, via homebrew), clang (13.1, apple/homebrew (both seem to be the same))



No comments:

Post a Comment