我代码中的核心函数如下所示(其他一切都是原始输入和输出):
const int n = 40000;
double * foo (double const * const x)
{
double * y = malloc (n*sizeof(double));
y[0] = x[0] + (0.2*x[1]*x[0] - x[2]*x[2]);
y[1] = x[1] + (0.2*x[1]*x[0] - x[2]*x[2]);
// …
// 39997 lines of similar code
// that cannot be simplified to fewer lines
// …
y[40000] = 0.5*x[40000] - x[12345] + 5*x[0];
return y;
}
假设为了这个问题的目的,像这样(或非常相似)硬编码这 40000 行确实是必要的。所有这些行都只包含固定数字和 x
条目的基本算术运算(平均每行 40 个);没有函数被调用。源的总大小为 14MB。
尝试编译这段代码时,我面临着编译器大量使用内存的问题。我可以让 Clang 使用 -O0
(只需要 20 秒)编译它,但我在 GCC(即使使用 -O0
)或 - 上失败了O1
.
虽然在代码端或全局范围内几乎没有什么可以优化的(即,通过以另一个顺序计算各个行),但我相信编译器会在局部范围内找到一些可以优化的东西(例如,计算计算 y[0]
和 y[1]
所需的括号项)。
因此我的问题是:
- 是否有一些编译器标志仅激活不需要太多额外内存的优化?
- 是否有其他一些方法可以使编译器更好地处理此源代码(不会损失比通过优化获得的速度更多的速度)?
最佳答案
The following comment Lee Daniel Crocker 解决了这个问题:
I suspect the limit you're running into is the size of the structures needed for a single stack frame/block/function. Try breaking it up into, say, 100 functions of 400 lines each and see if that does better.
当每个函数使用 100 行(并连续调用所有函数)时,我获得了一个可以用 -O2
编译的程序,没有任何问题。
关于c - 我如何编译这个非常大但无聊的 C 源代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33815231/