c - 将所有 C 代码写在一个源文件中真的会使程序运行得更快吗?

标签 c performance compilation

<分区>

我刚刚注意到在 sqlite 3 源代码文件的开头,它说:

/******************************************************************************
** This file is an amalgamation of many separate C source files from SQLite
** version 3.27.1.  By combining all the individual C code files into this
** single large file, the entire code can be compiled as a single translation
** unit.  This allows many compilers to do optimizations that would not be
** possible if the files were compiled separately.  Performance improvements
** of 5% or more are commonly seen when SQLite is compiled as a single
** translation unit.

我以前不知道这个,也找不到权威的来源来支持它。这是真的? C++ 呢?

最佳答案

它不会自动让它变快,但它有一定的道理。将所有内容都放在一个文件中允许编译器进行优化,否则是不可能的。例如,一个函数不能内联,除非它属于调用它的同一翻译单元

内联函数基本上意味着函数调用被函数体替换。这样做的一个好处是您可以跳过到函数代码的跳转和返回跳转。但是如果函数在另一个翻译单元中,那么编译器将只知道函数的原型(prototype)而不知道函数体,这又意味着它必须进行跳转。

话虽如此,我强烈建议不要使用这种方法。如果您真的需要最后的调整,那么使用某种可以从您的源代码树创建单个 c 文件的脚本。

关于c - 将所有 C 代码写在一个源文件中真的会使程序运行得更快吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54813021/

相关文章:

linux - 使用 --enable-compat15 编译 nfdump

c - csc 中 spmv 的 openmp 并行化

python - 什么是更快的操作,re.match/search 或 str.find?

ios - cocos2d中是否建议不要导入父类或兄弟类的头文件?

python - 迭代与列表串联

c# - 快速访问文件中的 key (无需将整个文件加载到内存中)

c++ - const 常量操作是否在运行时评估?

c - Arduino EEPROM 似乎不稳定,我应该写两次吗?

c++ - 从无符号长十六进制转换为 DWORD

计算均值中位数模式c编程数组