简体   繁体   English

像iostream这样的大型包含文件是否有效? (C ++)

[英]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. Iostream,当它包含的所有文件,包含它们的文件等等时,总计大约3000行。

Consider the hello world program, which needs no more functionality than to print something to the screen: 考虑一下hello world程序,它不需要比在屏幕上打印东西更多的功能:

#include <iostream> //+3000 lines right there.
int main()
{
    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. 这应该是一段非常简单的代码,但是iostream在边缘代码中增加了3000多行。 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? 那么,这些3000多行代码是否真的需要简单地在屏幕上显示一行,如果没有,他们是否创建了一个效率较低的程序,而不是简单地将相关行复制到代码中?

If you worry about the size of <iostream> when all you want is print a line of text, try <cstdio> and std::puts() . 如果你担心<iostream>的大小,只要打印一行文本,请尝试<cstdio>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...) (说真的,为什么人们使用printf()cout时更简单,更快速的puts()完全适合账单?它甚至会自动添加适当的换行...)

In a serious application, the size and compile time of <iostream> won't be significant. 严肃的应用程序中, <iostream>的大小和编译时间不会很大。 (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. 不,并非所有3000行都是真正需要打印代码行的,但是您会发现几乎不可能找到生成输出行所需的“几行”,因为I / O库源往往是严重相互依赖。 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. 除了增加编译时间之外,它们不会造成损害 - 因为在链接器阶段丢弃“绒毛”,所以代码的效率不会降低。

它使编译变慢(但可以使用预编译的头文件等减轻),但是任何体面的链接器都应该删除不需要的东西。

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). 关于性能如果您在许多cpp文件中使用了许多大头文件,请考虑使用预编译头文件,这将大大提高编译时间性能(包含的头文件已预先解析并重新使用)。

在C ++中,代码中未调用的函数不会被编译。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM