开发者

Are large include files like iostream efficient? (C++)

Iostream, when all of the files it includes, the files that those include, and so on and so forth, adds up to about 3000 lines.

Consider the hello world program, which needs no more functionality than to print something to the screen:

#include <iostream> //+3000 lines right there.
int main()开发者_JAVA技巧
{
    std::cout << "Hello, World!";
    return 0;
}

this should be a very simple piece of code, but iostream adds 3000+ lines to a marginal piece of code. So, are these 3000+ lines of code really needed to simply display a single line to the screen, and if not, do they create a less efficient program than if I simply copied the relevant lines into the code?


If you worry about the size of <iostream> when all you want is print a line of text, try <cstdio> and std::puts().

(Seriously, why do people use printf() or cout when the much simpler and quicker puts() fits the bill perfectly? It even appends an appropriate line feed automatically...)

In a serious application, the size and compile time of <iostream> won't be significant. (Plus, as others already noted, the linker won't link in what isn't used.)

Edit: I just realized I didn't really answer the question. No, not all the 3000 lines are really required to print the line of code, but you'll find it next to impossible to find "the few lines" required to produce your line of output, as I/O library source tends to be heavily interdependent. And aside from increasing compile times a bit, they don't harm - your code does not get any less efficient as the "fluff" is dropped at linker stage.


It makes the compile slow (but that can be mitigated with precompiled headers etc), however any decent linker should remove the stuff that's not needed.


The compiler removed everything that is not needed during the link step. Be confident on the compiler there is no need for a manual cleaning! Regarding performance if you have many big headers you use in many cpp files, consider using precompiled headers that'll boost up compile time performance a lot (the included headers are pre-parsed and re-used).


in C++, functions which are not called in your code don't get compiled.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜