相关疑难解决方法(0)

在g ++上使用聚合初始化的std :: array会生成巨大的代码

在g ++ 4.9.2和5.3.1上,此代码需要几秒钟的时间来编译并生成52,776字节的可执行文件:

#include <array>
#include <iostream>

int main()
{
    constexpr std::size_t size = 4096;

    struct S
    {
        float f;
        S() : f(0.0f) {}
    };

    std::array<S, size> a = {};  // <-- note aggregate initialization

    for (auto& e : a)
        std::cerr << e.f;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

增加size似乎线性增加编译时间和可执行文件大小.我无法使用clang 3.5或Visual C++ 2015重现此行为.使用-Os没有区别.

$ time g++ -O2 -std=c++11 test.cpp
real    0m4.178s
user    0m4.060s
sys     0m0.068s
Run Code Online (Sandbox Code Playgroud)

检查汇编代码显示初始化a已展开,生成4096 movl条指令:

main:
.LFB1313:
    .cfi_startproc
    pushq   %rbx
    .cfi_def_cfa_offset 16
    .cfi_offset …
Run Code Online (Sandbox Code Playgroud)

c++ optimization g++ loop-unrolling stdarray

24
推荐指数
1
解决办法
708
查看次数

标签 统计

c++ ×1

g++ ×1

loop-unrolling ×1

optimization ×1

stdarray ×1