Bo *_*o R 1 c++ performance g++ c++11 c++14
从 C++11 迁移到 C++14,我有一个源文件,通常在 C++11 模式下编译大约 20 秒,但在 C++14 模式下编译同一文件时,编译时间增加到大约 340 秒。增幅约为 17 倍。生成的目标代码的大小加倍。
因此,为了澄清我的问题,我试图理解代码中的内容以及为什么编译器通过-std=c++11/c++14更改g++.
为了进行公平的比较,在代码上运行了预处理器(在 C++11 模式下),并使用 C++11 和 C++14 标志编译输出。例如,
g++ -E -std=c++11 -g -O2 -o spaghetti.E spaghetti.cpp
g++ -c -std=c++11 -g -O2 -o spaghetti-11.o -x c++ spaghetti.E
g++ -c -std=c++14 -g -O2 -o spaghetti-14.o -x c++ spaghetti.E
Run Code Online (Sandbox Code Playgroud)
有问题的文件确实有很多模板形式的固定大小的对象数组,例如
template<typename T, std::size_t N>
struct Object {
std::array<T,N> storage{};
std::vector<T> counters;
};
Run Code Online (Sandbox Code Playgroud)
Object当使用 C++14 而非 C++11(以及其他)编译时,涉及 do double 的符号。
那么,是什么促使编译器花费 17 倍的时间来编译同一个文件呢?应该改变什么来减少这种情况?
我应该注意,如果我改变Object实施
template<typename T, std::size_t N>
struct Object {
std::vector<T> storage{}; // was std::array above, N used in other members
std::vector<T> counters;
};
Run Code Online (Sandbox Code Playgroud)
它在 C++11 和 C++14 下都能快速编译。