Including extra headers that I do not really need

I always wondered what are the effects of including not needed headers to the final executable once compiled. In my code some times I may included many different headers that I do not need (or I used to need but now I don’t and forgot to remove them). Now before compiling the final executable I always clean up my header files. But I am wondering, is there a problem if I don’t?

I am aware that in C# having unused Using in my code only reduces (slightly) the speed of intellisense and the compiler only includes stuff that is really needed. Is the same thing true for C and C++? What about Java and other languages?

Importing in Java, C# and the like is fundamentally different from including in C and C++:

The former will add the named modules to those considered for symbol resolution, the latter inserts the named headers content literally into the translation unit.
Which means a header can contain anything the source-file can, including translation-unit-local objects (They might have a constructor and destructor in C++), functions, structs (and for C++ classes).

Which is not quite as bad as it sounds for C, as headers tend to mostly contain forward-declarations, struct definitions and preprocessor-macros.

In C++ on the other hand, templates (which are far more powerful than puny Java or C# generics) generally must be implemented in headers, which makes them grow to considerable size and complexity (often outstripping by orders of magnitude the source-file of the translation-unit).

The C++ committee is working on a module-system to take care of that, but at the earliest it will be there in C++17 (and don’t hold your breath yet).

So, for the languages with module-system (Java, C#) you will only have a slight slowdown for analysis and compilation.
For C, you will probably only have a slightly higher slowdown, due to literal inclusion of the source, though at least they tend not to be too big and complex.
For C++, you will have a moderate to severe slowdown of compilation and analysis, due to heavy use of templates and inline-functions.

In both C and C++ headers can in theory contain TU-local objects, which would result in some runtime overhead.
While I know that such is used by C++ for <iostream> (look for ios_base::Init in the standard) to initialize the C++ streams early enough, I don’t know of any use in C.

0

If you are not using precompiled headers then each include means that the header is included fully into each compilation unit. That means reading the file and putting it through the same pipeline of the rest of the code and filling the symbol table.

So yeah unused includes will slow down compilation.

Some compilers will have optimizations in place that alleviates most of the parsing work by creating precompiled headers that has just the symbols and templates in a proprietary easy to parse format. However doing the lookup each #include will still take some time.

Java’s imports are a bit different, when you import a package (for example import java.util.*) then the compiler will look through the directory/jar specified in the class path and make a map of the public classes in it so ArrayList gets translated to java.util.ArrayList. Similarly for importing a class; it gets looked up and added to the translation table. Doing a static import requires looking in the class file and parsing the public declarations in it.

One more downside in C/++ to including an unneeded header in a source file: if precompiled headers are not used, then changing that unneeded header file forces recompilation of that source file.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật