c++ - 使用大型库是否会固有地使代码变慢?

标签 c++ c performance boost glib

我有一种心理抽搐,这让我不愿意在 C 和 C++ 等低级语言中使用大型库(如 GLibBoost )。在我看来,我认为:

Well, this library has thousands of man hours put into it, and it's been created by people who know a lot more about the language than I ever will. Their authors and fans say that the libraries are fast and reliable, and the functionality looks really useful, and it will certainly stop me from (badly) reinventing wheels.

But damn it, I'm never going to use every function in that library. It's too big and it's probably become bloated over the years; it's another ball and chain my program needs to drag around.

Torvalds rant (尽管有争议)也不能让我放心。

我的想法有什么根据,还是我只是不合理和/或无知?即使我只使用大型库的一两个功能,通过链接到该库是否会产生运行时性能开销?

我确信这也取决于具体的库是什么,但我通常想知道大型库是否会在技术层面上固有地引入低效率。

当我没有技术知识来判断我是否正确时,我已经厌倦了痴迷、喃喃自语和担心。

请让我摆脱痛苦!

最佳答案

Even if I only use one or two features of a large library, by linking to that library am I going to incur runtime performance overheads?

一般来说不会。

如果所讨论的库没有很多与位置无关的代码,那么当动态链接器在请求时对库执行重定位时,就会产生启动成本。通常,这是程序启动的一部分。除此之外没有运行时性能影响。

链接器还擅长在构建时从静态链接库中删除“死代码”,因此您使用的任何静态库都将具有最小的大小开销。性能甚至不考虑它。

坦率地说,您担心的是错误的事情。

关于c++ - 使用大型库是否会固有地使代码变慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2247465/

相关文章:

c - 打印调用函数的文件名、行号和函数名 - C 程序

c - 三元运算是否在编译时进行?

c - 帮助使此代码针对 SPOJ 运行得更快

performance - 强制整个站点使用 HTTPS 的好习惯还是坏习惯?

c++ - 对齐访问数组元素

c++ - 如何允许多个线程访问 C++ 中匹配条件的临界区

c# - 自动执行 C++ 库的 C# 包装

c++ - 将枚举数组转换为 int 指针

c - 多线程程序中的输出

c - 右排列文本中的单词