C++ 31 Aug 2011 22:27:06

A performance benchmark of which include guard method is faster. Test times the compilation of a main.cpp that includes 10000 files 3 times each.

The tested methods are:

#pragma once followed by #ifndef

followed by #ifndef followed by #pragma once

followed by Only #pragma once

Only #ifndef

External #ifndef

Sources for the test is at inc.tar.gz, but it’s just 5×10000 files.

The compilers are Microsoft Visual C++ 2010, GNU g++ 4.6.1, and LLVM clang++ 2.9.

Windows: MSVC++ 2010

Compiler: MSVC++ 2010, cl.exe /O2

Arch: Windows 7 64 bit, 1.60GHz Core i7 Q720, 8 GiB RAM

VC++ 2010 Seconds Pragma + ifndef 3.687 Ifndef + pragma 3.744 Pragma only 3.597 Ifndef only 5.153 External 2.673

Linux: GNU g++ 4.6.1

Compiler: GNU g++ 4.6.1 -O3

Arch: VirtualBox on the Windows machine, VT-x, Arch Linux x86_64, kernel 3.0-ARCH, 1 GiB RAM

g++ 4.6.1 Seconds Pragma + ifndef 2.007 Ifndef + pragma 1.804 Pragma only 2.005 Ifndef only 0.309 External 0.313

Linux: LLVM clang++ 2.9

Compiler: clang++ 2.9 -O3

Arch: VirtualBox on the Windows machine, VT-x, Arch Linux x86_64, kernel 3.0-ARCH, 1 GiB RAM