简体   繁体   English

哪个更快的C ++:完全编译,或链接到共享或静态库?

[英]Which is faster in C++: compiling altogether, or linking to shared or static library?

There are several discussions on forums about shared vs. static libraries regarding performance. 在论坛上有几个关于性能的共享库和静态库的讨论。 But how do those approaches compare to compiling the code altogether? 但是这些方法与完全编译代码相比如何呢?

In my case, I have a class (the evaluation code ) that contains a few methods that contain several for loops and that will be called several times by a method from another class (the evaluator code ). 在我的例子中,我有一个类( 评估代码 ),它包含一些包含多个for循环的方法,并且将通过另一个类的方法( 评估程序代码 )多次调用。 I have not finished implementing and testing everything yet. 我还没有完成实施和测试。 But, for the sake of performance, I am wondering if I should compile all the files altogether (compiler optimization advantages?), or compile some files separately to generate static or shared libraries. 但是,为了性能,我想知道我是否应该完全编译所有文件(编译器优化优势?),或者单独编译一些文件以生成静态或共享库。

These approaches will depend on your compiler and options: 这些方法取决于您的编译器和选项:

Not using libraries: A good compiler, and build system will cache results and this should be just as fast as the other two. 不使用库:一个好的编译器,构建系统将缓存结果,这应该和其他两个一样快。 In practice, many code bases have less than optimal compartmentalization leading to slow compile times, the classic approach is to break the thing apart into libraries. 在实践中,许多代码库没有最佳的分区,导致编译时间慢,经典的方法是将事物分解为库。

Static: This might be slower than dynamic linking because there is an opportunity to run link time optimization (LTO), which could take a while 静态:这可能比动态链接慢,因为有机会运行链接时间优化(LTO),这可能需要一段时间

Dynamic: Might be slower when you have a small number of functions because of specifics on how dynamic loading is implemented. 动态:由于有关如何实现动态加载的细节,当您拥有少量函数时可能会更慢。

In conclusion, unless you're working on some monster project where you're worried about people truking up the build system, keep it all in one project and avoid needlessly complicated debugging. 总而言之,除非你正在开展一些怪物项目,你担心人们会占用构建系统,否则请将它们保存在一个项目中,避免不必要的复杂调试。

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

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