包含库的运行时性能成本是多少? [英] What is the runtime performance cost of including a library?

查看:114
本文介绍了包含库的运行时性能成本是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在包含整个库(可能有数百个函数)之间是否存在运行时性能差异,然后只使用一个函数,如:

Is there any runtime performance difference between including an entire library (with probably hundreds of functions) and then using only a single function like:

#include<foo>

int main(int argc, char *argv[]) {
    bar();//from library foo
    return 0;
}

在粘贴相关代码片段之间库直接进入代码,例如:

And between pasting the relevant code fragment from the library directly into the code, like:

void bar() {
...
}

int main(int argc, char *argv[]) {
    bar();//defined just above
    return 0;
}

什么会阻止我盲目地包括我最喜欢的所有(最常用的) )我的C文件开头的库?这个受欢迎的主题 C / C ++:检测多余的#includes?表明编译时间会增加。但编译后的二进制文件会有什么不同吗?第二个程序实际上是否会优于第一个程序?

What would prevent me from mindlessly including all of my favourite (and most frequently used) libraries in the beginning of my C files? This popular thread C/C++: Detecting superfluous #includes? suggests that the compilation time would increase. But would the compiled binary be any different? Would the second program actually outperform the first one?

相关: #include< stdio.h>真的在交流计划中做什么

编辑:这里的问题与相关的问题不同在C / C ++中包含未使用的头文件是否会受到性能影响?问题为这里有一个单个文件。我在这里问,如果包含一个文件与将实际使用的代码片段复制粘贴到源中有任何不同。我稍微调整了标题以反映这种差异。

the question here is different from the related Will there be a performance hit on including unused header files in C/C++? question as here there is a single file included. I am asking here if including a single file is any different from copy-pasting the actually used code fragments into the source. I have slightly adjusted the title to reflect this difference.

推荐答案

就最终程序而言,没有任何性能差异。链接器仅链接实际用于程序的函数。库中存在的未使用函数将无法链接。

There is no performance difference as far as the final program is concerned. The linker will only link functions that are actually used to your program. Unused functions present in the library will not get linked.

如果包含大量库,则编译程序可能需要更长时间。

If you include a lot of libraries, it might take longer time to compile the program.

你不应该包括所有最喜欢的图书馆的主要原因是程序设计。您的文件不应包含除正在使用的资源之外的任何内容,以减少文件之间的依赖关系。你的文件对程序其余部分的了解越少越好。它应该尽可能自主。

The main reason why you shouldn't include all your "favourite libraries" is program design. Your file shouldn't include anything except the resources it is using, to reduce dependencies between files. The less your file knows about the rest of the program, the better. It should be as autonomous as possible.

这篇关于包含库的运行时性能成本是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆