#pragma once
vs. #ifndef
/#define
Code
After getting in an extended discussion about the supposed performance tradeoff between #pragma once
and #ifndef
guards vs. the argument of correctness or not (I was taking the side of #pragma once
based on some relatively recent indoctrination to that end), I decided to finally test the theory that #pragma once
is faster because the compiler doesn’t have to try to re-#include
a file that had already been included.
For the test, I automatically generated 500 header files with complex interdependencies, and had a .c
file that #include
s them all. I ran the test three ways, once with just #ifndef
, once with just #pragma once
, and once with both. I performed the test on a fairly modern system (a 2014 MacBook Pro running OSX, using XCode’s bundled Clang, with the internal SSD).