Skip to content

theamigoooooo/pch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CMake Precompiled Header (PCH) Demo

This project demonstrates the impact of Precompiled Headers (PCH) on C++ compile times using CMake.

It builds the same set of source files twice:

  1. Without PCHtest_no_pch
  2. With PCHtest_with_pch

By comparing build times, you can see how much precompiled headers reduce compilation time in larger C++ projects.


Project Structure

.
├── CMakeLists.txt
├── pch.hpp
├── heavy.hpp
├── file1.cpp
├── file2.cpp
├── ...
├── include/
└── README.md

Important Files

  • pch.hpp Contains headers that are expensive to compile and are shared across many source files.

  • heavy.hpp Simulates a heavy header with many includes.

  • file.cpp* Multiple source files that include the heavy headers.


Requirements

  • CMake 3.20+

  • A C++ compiler supporting C++20

    • GCC
    • Clang
    • MSVC

Building the Project

Create a build directory and configure the project:

mkdir build
cd build
cmake ..

Build the executables:

cmake --build .

This will generate two binaries:

test_no_pch
test_with_pch

Measuring Compile Time

To measure compilation performance, you can use the time command.

Without Precompiled Headers

time cmake --build build --target test_no_pch

With Precompiled Headers

time cmake --build build --target test_with_pch

You should observe faster compile times with PCH, especially when many source files include the same heavy headers.


Why Precompiled Headers?

Large C++ projects often include the same headers across hundreds of files.

Examples:

  • <vector>
  • <string>
  • <iostream>
  • <algorithm>

Compilers must normally re-parse these headers for every .cpp file.

Precompiled Headers solve this by:

  1. Compiling commonly used headers once
  2. Reusing the compiled representation for all source files

This can significantly reduce build times in large projects.


Example PCH Usage in CMake

CMake provides built-in support:

target_precompile_headers(test_with_pch
    PRIVATE pch.hpp
)

This instructs CMake to generate a precompiled header from pch.hpp and use it for the target.


Generating Many Test Files (Optional)

To stress test compilation performance, you can generate many source files:

for i in {1..100}; do
cat <<EOF > file$i.cpp
#include "heavy.hpp"

int func$i()
{
    std::vector<int> v = {1,2,3,4,5};
    return v.size();
}
EOF
done

Key Takeaway

Precompiled headers can dramatically reduce compile times when:

  • Many translation units include the same headers
  • Headers are large or template-heavy
  • The project contains hundreds of source files

However, PCH must be managed carefully to avoid unnecessary rebuilds.


License

This project is intended for educational and benchmarking purposes.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors