C++ 31 Aug 2011 22:27:06

C++ Include Speed

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
  • #ifndef followed by #pragma once
  • 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++ 2010Seconds
Pragma + ifndef3.687
Ifndef + pragma3.744
Pragma only3.597
Ifndef only5.153
External2.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.1Seconds
Pragma + ifndef2.007
Ifndef + pragma1.804
Pragma only2.005
Ifndef only0.309
External0.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
clang++ 2.9Seconds
Pragma + ifndef0.580
Ifndef + pragma0.578
Pragma only0.532
Ifndef only0.568
External0.581
StumbleUponFacebookDiggDeliciousRedditMySpaceShare

2 Responses to “C++ Include Speed”

  1. on 01 Sep 2011 at 20:35:25 1.Nick Porcino said …

    Is it possible that file caching or other VM behaviors by VirtualBox is skewing the results from gcc and clang?

  2. on 01 Sep 2011 at 20:51:59 2.Tino Didriksen said …

    I thought of that, so I tested it on a real machine and got same relative results. And I did run every compilation 5-7 times to make sure the files were well cached.

    Anyway, the conclusion is that it doesn’t matter. If the difference for 10000 almost empty files is a second or two more, then your real world project won’t care whatsoever which include method you use.

Subscribe to the comments through RSS Feed

Leave a Reply

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image