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++ 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
clang++ 2.9 | Seconds |
---|---|
Pragma + ifndef | 0.580 |
Ifndef + pragma | 0.578 |
Pragma only | 0.532 |
Ifndef only | 0.568 |
External | 0.581 |
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?
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.
on 27 Jun 2014 at 18:33:18 3.John Doe said …
Two things:
1. If you tested on an actual machine, why not edit the post with those times instead?
2. In the Windows you’re running, what else are you running? Anti-virus, hooks, explorer extensions, etc? Can you feasibly run in a fresh W7 with a fresh VC++2010?
on 27 Jun 2014 at 21:42:17 4.Foster Brereton said …
The question I am interested in getting answered is why is MSVC almost 10x slower in the #ifndef case than clang, and what can be done to mitigate that cost?
on 28 Jun 2014 at 10:30:22 5.Tino Didriksen said …
Since I got the same relative results, it won’t matter if I put those numbers in. They’ll have the same factors to the other numbers.
It doesn’t matter what the Windows machine was running, as I redid the tests several times and got the same numbers. Also, since the VBox was running on the Windows machine, any host slowdown would also affect the guests – especially I/O slowdowns. Since there is no seen I/O slowdown, and since tests are run multiple times to cache files, one can conclude that raw I/O throughput is not the issue. But, no active AV.
And again, test is for 3x 10000 files. For a single file, MSVC’s slowest case would thus be somewhere between 0.0005153 and 0.0001718 seconds – also known as 0.5 and 0.2 milliseconds.
There is no cost to mitigate. The conclusion is that include speed is super fast in ALL compilers.