It entirely depends on what/how you are building. The project I’m currently working on is a mid-sized one, so without PCH it takes about 20 minutes to build the complete set of primary targets on an i7 11k/64gb ddr4 @4k, industrial nvme 3rd gen with visual studio.
Adding the precompiled headers to 20 of the 480 targets reduced the compile time to 6-9 minutes.
pch won’t improve - often worsens - cold build times for relatively small projects, because it still has to do all that compiling.
Whether to use pch or not should start with a little inspection of what exactly you are including. For instance, if C++'s or or have crept into your common headers, they can individually add 10-100ms per compilation unit on gcc/clang, 40-200ms on msvc.
If, on the other hand, you have an excellent separation of concerns so that very few compilation units are including the same headers, PCH won’t help you - infact it will make it worse.
And rarely will putting all your headers into a pch help. What you actually want to target are the headers that are accessed most often and consume the most front-end compile time.
E.g, if you know that every .cpp in your project is going to want , , , , and most of them want , create a “pch.h” file with those listed, and make that your precompiled header file - that will force every compilation unit to precompile it, and then those costly files won’t need to be parsed each time.
Definitely do not add trivial header files:
#pragma once
#ifndef THIS_INCLUDE_FILE
#define THIS_INCLUDE_FILE
namespace Ours
{
enum States
{
On, Off
};
}
#endif
the cost of pching that will outweigh the cost of including in it upto 12 compilation units.
If your compile times are problematic, you may want to consider a Jumbo/Unity build instead UNITY_BUILD — CMake 3.26.0-rc1 Documentation but again, that has caveats.
A final option you might want to look at is Ccache — Compiler cache and/or distcc: a fast, free distributed C/C++ compiler.
Ccache is a no-brainer on Linux, almost instantaneous benefits for smaller projects. The amount of supervision increases with project scale, although I’ve yet to run into any of the weird edge-cases people see and I’ve worked on some doozies (Blizzard/Facebook/SpaceX).
Ccache has some gotchas on MacOS (it doesn’t do well with dual-architecture, Xcode chose to do it in a specific way which is cache-defeating), and it’s supposed to work on Windows but I’m not sure if that’s mingw specific - I haven’t gotten around to setting it up.
— edit
Addenda: If compilation speed is a major factor for you, you may want to consider golang. The compiler is so fast that you can work on the docker source in vscode yet have the compiler build the entire project fast enough to be how they handle tooltip/intellisense.