Блог пользователя ilia_rr

Автор ilia_rr, 3 года назад, По-английски

Hi everybody!

This blog is about speeding up C/C++ compilers with precompiled headers.

Including lots of headers or big headers like bits/stdc++.h will increase compilation time. This can be annoying even with high speed processors. We can solve this using precompiled headers.

What are precompiled headers?

You can compile a header like stdc++.h to stdc++.h.gch and compiler will use it instead of compiling the header every time you are compiling your code. Less processing => Less time.

How to use precompiled headers?

  1. Find your g++ default include directory

    • Ubuntu: /usr/include/x86_64-linux-gnu/c++/{version}
    • Windows: C:\MinGW\lib\gcc\mingw32\{version}\include\c++
  2. Compile headers you need (I recommend using bits/stdc++.h instead of including lots of headers).

    g++ {header name} {flags you use when compiling a normal code}

  3. Put .gch files in the right place. You have 2 Options:

    • Put them in the directory you find the headers & use #include <header name>
    • Put them in your code directory & use #include "header name"

    I recommend first options because the second one will work only when .gch files are in the code directory and it takes a little extra time to search in code.

Compile a code and feel the difference!

Comment your issues or suggestions.

Be good and code fast ;)

  • Проголосовать: нравится
  • +44
  • Проголосовать: не нравится

»
3 года назад, # |
Rev. 3   Проголосовать: нравится -49 Проголосовать: не нравится

plaese don't down bruh

»
3 года назад, # |
  Проголосовать: нравится +5 Проголосовать: не нравится

My own experience:

I use precompiled stdc++.h in the default header directory and compile time decreased from 600ms to 100ms.

»
3 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Thanks for it. But it dont include windows.h library. Thanks already.

»
3 года назад, # |
Rev. 3   Проголосовать: нравится -26 Проголосовать: не нравится

please don't down vote bruh

»
3 года назад, # |
  Проголосовать: нравится +1 Проголосовать: не нравится

mine got improved from 2.376 seconds to 0.692 seconds, many thanks

»
3 года назад, # |
  Проголосовать: нравится +5 Проголосовать: не нравится

Thanks for your helpful blog (◕ᴗ◕✿)

»
3 года назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

stdc++.h.gch file has been created in the same folder where the original stdc++.h file is. But still this isn't speeding up compilation. (I am using code runner extension on VS code.)

Am I missing something? :(

  • »
    »
    3 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    Are you sure you found the right directory?

    Use g++ -xc++ -E -v - to find it.

    • »
      »
      »
      3 года назад, # ^ |
      Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

      The VS code extension is running a command g++ filename.cpp -o filename.exewhile compiling and running. So am I supposed to add -o as a flag in command given in the blog.

      Update : NVM, now it is taking 2 seconds to compile, previously it was taking 7-8 seconds. My laptop processor might be the bottleneck, coz it is too old. But yeah, I see this as an absolute win :)

»
3 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

If you don't want to clutter your system directories with .gch files that may become outdated, you can also precompile bits/stdc++.h, save the resulting .gch file where you prefer and add -include <gch file path, without .gch at the end> to your g++ command line.

»
8 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Thanks for your blog, honestly I tried to apply these things and other things from different blogs but it didn't work. Finally I knew the problem.

For those who couldn't solve the problem, open (Project) -> (Build Options), from the menu to the left select (debug) and from the (Policy) select (Use project options only). This worked for me.

Enjoy ;)