For example, if I have:
#include <iostream>
int main() {}
vs
int main() {}
Will there be a measurable difference?
Does the compiler ignore unused includes anyway?
What I've tried:
<iostream>
, but I don't have a speed test in my IDE to check if it is worked.Removing #include <iostream>
will have the following effects:
Compilation time for each translation unit that no longer has #include <iostream>
will be slightly reduced.
The reason:
The compiler does not have to process the rather big header file.
The program startup time and executable size might be slightly reduced.
The reason:
If you remove #include <iostream>
from all your translation units, it means that the global standard stream objects will not be present in your executable.
This in turn means that they do occupy space in the executable and do not need to be initialized when the program starts, and the effect is a slightly faster startup (which I believe is barely measurable).
Note: some linkers may remove the stream objects if they are never used anyway, so the above might not be relevant for them.
For #include
s other than <iostream>
that do not introduce global objects - only effect #1 above (reduce compilation time) is relevant.
Bottom line:
You might get a very minimal improvement in the program startup time.
But good practice dictates #include
ing only what you actually use anyway.
This will make your source files cleaner and will possibly reduce compilation times (as well as program startup time and size).