const 数组的 C++ 编译时间长
C++ long compilation time of const array
我正在 Node.js 使用 C++ 组件制作游戏。这个 C++ 组件经常被调用(作为子进程)并且是一个瓶颈。因此,我将其资产硬编码为 const 数组,以使其 运行 更快。
但是,我注意到我的 C++ 代码需要很长时间才能编译。以下是问题的演示:
#include <iostream>
#include <vector>
#define d 3,
#define _ 999,
// an array holding some game assets
const std::vector<std::vector<std::vector<int>>> STRUCTURES {
{ // you should make 80 copies of this paragraph before benchmarking
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ d d d d d d d _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ d d d d d d d d d d d _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d d _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d d d _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d d d d _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d d d d _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d d _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d d _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d d d _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d d d _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d d d _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d d d _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d d d d _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d d d d _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d d d d _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d d d d _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d d d _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d d d _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d d _ _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d _ _ _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ d d d d d d d d d d d d d d d _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ d d d d d d d d d d d _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ d d d d d d d _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ },
},
};
int main(int argc, char **argv){
// prevent optimizer from ignoring the big array
std::cout << STRUCTURES.size() << std::endl;
return 0;
}
如果您将中间段落重复 80 次,它的大小将与我的 C++ 程序相当。我用 g++ test.cpp -O3
编译,编译需要 36 秒。我不明白为什么这么慢。有什么我可以改变的,让它编译得更快吗?
按照 Barmar 和 Sam Varshavchik 的建议,解决方案是避免 std::vector。
向量太多,优化器需要花费大量时间来处理。 std::array 可以代替。对于此示例,这意味着:
#include <iostream>
#include <vector>
#include <array>
#define d 3,
#define _ 999,
// an array holding some game assets
const std::vector<std::array<std::array<int,37>,37>> STRUCTURES {
{{ // you should make 80 copies of this paragraph before benchmarking
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ d d d d d d d _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ d d d d d d d d d d d _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d d _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d d d _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d d d d _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d d d d _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d d _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d d _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d d d _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d d d _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d d d _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d d d _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d d d d _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d d d d _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d d d d _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d d d d _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d d d _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d d d _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d d _ _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d _ _ _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ d d d d d d d d d d d d d d d _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ d d d d d d d d d d d _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ d d d d d d d _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ },
}},
};
int main(int argc, char **argv){
// prevent optimizer from ignoring the big array
std::cout << STRUCTURES.size() << std::endl;
return 0;
}
请注意,我仍然使用单个 std::vector 作为最外层维度。这在编译时不会产生明显的开销。
不使用动态分配有帮助:
template <typename T, int a, int b, int c>
using sprites = std::array<std::array<std::array<T, c>, b>, a>;
但完全不分配肯定更快。现在我不了解你,但我发现初始化程序正确并且排列非常乏味 (TM) 所以,让我们不要:
// an array holding some game assets
static constexpr char STRUCTURES[] {
// pos 0
"_____________________________________"
"_____________________________________"
"_____________________________________"
"_____________________________________"
"_________________________ddddddd_____"
"______________________ddddddddddd____"
"_____________________ddddddddddddd___"
"____________________dddddddddddddd___"
"___________________ddddddddddddddd___"
"__________________dddddddddddddddd___"
"_________________dddddddddddddddd____"
"________________ddddddddddddddddd____"
"_______________ddddddddddddddddd_____"
"______________ddddddddddddddddd______"
"____________dddddddddddddddddd_______"
"___________dddddddddddddddddd________"
"__________ddddddddddddddddddd________"
"_________ddddddddddddddddddd_________"
"________ddddddddddddddddddd__________"
"________ddddddddddddddddddd__________"
"_______dddddddddddddddddddd__________"
"______dddddddddddddddddddd___________"
"______dddddddddddddddddddd___________"
"______dddddddddddddddddddd___________"
"______ddddddddddddddddddd____________"
"______ddddddddddddddddddd____________"
"______dddddddddddddddddd_____________"
"______ddddddddddddddddd______________"
"_______ddddddddddddddd_______________"
"________ddddddddddddd________________"
"_________ddddddddddd_________________"
"___________ddddddd___________________"
"_____________________________________"
"_____________________________________"
"_____________________________________"
"_____________________________________"
"_____________________________________"
// pos 1
// ....
// ....
// ....
"_____________________________________"
"_____________________________________"
// pos 81
"_____________________________________"
"_____________________________________"
"_____________________________________"
"_____________________________________"
"_________________________ddddddd_____"
"______________________ddddddddddd____"
"_____________________ddddddddddddd___"
"____________________dddddddddddddd___"
"___________________ddddddddddddddd___"
"__________________dddddddddddddddd___"
"_________________dddddddddddddddd____"
"________________ddddddddddddddddd____"
"_______________ddddddddddddddddd_____"
"______________ddddddddddddddddd______"
"____________dddddddddddddddddd_______"
"___________dddddddddddddddddd________"
"__________ddddddddddddddddddd________"
"_________ddddddddddddddddddd_________"
"________ddddddddddddddddddd__________"
"________ddddddddddddddddddd__________"
"_______dddddddddddddddddddd__________"
"______dddddddddddddddddddd___________"
"______dddddddddddddddddddd___________"
"______dddddddddddddddddddd___________"
"______ddddddddddddddddddd____________"
"______ddddddddddddddddddd____________"
"______dddddddddddddddddd_____________"
"______ddddddddddddddddd______________"
"_______ddddddddddddddd_______________"
"________ddddddddddddd________________"
"_________ddddddddddd_________________"
"___________ddddddd___________________"
"_____________________________________"
"_____________________________________"
"_____________________________________"
"_____________________________________"
"_____________________________________"
};
朴素的索引助手
现在,当然,您需要整数并将字符映射到某些值(例如本例中的 3 和 999)。为此,我们可以使用一个简单的索引助手:
struct Helper {
constexpr int operator()(int a, int b, int c) const {
switch (auto ch = STRUCTURES[a*37*37 + b*37 + c]) {
case 'd': return 999;
case '_': return 3;
}
return 0;
}
};
现在我们可以编写 main
来打印随机“字形”、“精灵”或任何这些资源:
#include <iostream>
#include <iomanip>
int main() {
auto a = rand() % 81;
Helper index;
for (int b = 0; b < 37; ++b) {
for (int c = 0; c < 37; ++c)
std::cout << std::setw(4) << index(a, b, c);
std::cout << "\n";
}
}
它打印(我有 81 倍相同的“图像”):
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 999 999 999 999 999 999 999 3 3 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 999 999 999 999 999 999 999 999 999 999 999 3 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 999 999 999 999 999 999 999 999 999 999 999 999 999 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 999 999 999 999 999 999 999 999 999 999 999 999 999 999 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 3 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 3 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 3 3 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 3 3 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 3 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 3 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 3 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 3 3 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 3 3 3 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 999 999 999 999 999 999 999 999 999 999 999 999 999 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 999 999 999 999 999 999 999 999 999 999 999 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 3 3 999 999 999 999 999 999 999 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
非朴素索引助手:Boost MultiArray
更少的代码,更多的功能。现在你可以使用 .size()
和 ranged-for:
#include <boost/multi_array.hpp>
int main() {
constexpr std::array shape {81,37,37};
boost::const_multi_array_ref<char, 3> view(STRUCTURES, shape);
auto const& sprite = view[rand() % view.size()];
for (auto const& row : sprite) {
for (auto const& cell : row)
printf("%4d", (cell=='d'? 999 : 3));
putchar('\n');
}
}
现场完整演示
朴素索引 Helper
版本:Live On Coliru
编译:(在 coliru 上!)
real 0m0.562s
user 0m0.432s
sys 0m0.124s
运行时(在 coliru 上!):
real 0m0.005s
user 0m0.000s
sys 0m0.004s
提升多阵列版本:Compiler Explorer/Coliru
我的系统(两种方法)上的可执行文件大小为 118k (GCC) 或 119k (clang)。
我正在 Node.js 使用 C++ 组件制作游戏。这个 C++ 组件经常被调用(作为子进程)并且是一个瓶颈。因此,我将其资产硬编码为 const 数组,以使其 运行 更快。
但是,我注意到我的 C++ 代码需要很长时间才能编译。以下是问题的演示:
#include <iostream>
#include <vector>
#define d 3,
#define _ 999,
// an array holding some game assets
const std::vector<std::vector<std::vector<int>>> STRUCTURES {
{ // you should make 80 copies of this paragraph before benchmarking
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ d d d d d d d _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ d d d d d d d d d d d _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d d _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d d d _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d d d d _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d d d d _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d d _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d d _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d d d _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d d d _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d d d _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d d d _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d d d d _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d d d d _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d d d d _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d d d d _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d d d _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d d d _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d d _ _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d _ _ _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ d d d d d d d d d d d d d d d _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ d d d d d d d d d d d _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ d d d d d d d _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ },
},
};
int main(int argc, char **argv){
// prevent optimizer from ignoring the big array
std::cout << STRUCTURES.size() << std::endl;
return 0;
}
如果您将中间段落重复 80 次,它的大小将与我的 C++ 程序相当。我用 g++ test.cpp -O3
编译,编译需要 36 秒。我不明白为什么这么慢。有什么我可以改变的,让它编译得更快吗?
按照 Barmar 和 Sam Varshavchik 的建议,解决方案是避免 std::vector。
向量太多,优化器需要花费大量时间来处理。 std::array 可以代替。对于此示例,这意味着:
#include <iostream>
#include <vector>
#include <array>
#define d 3,
#define _ 999,
// an array holding some game assets
const std::vector<std::array<std::array<int,37>,37>> STRUCTURES {
{{ // you should make 80 copies of this paragraph before benchmarking
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ d d d d d d d _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ d d d d d d d d d d d _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d d _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d d d _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d d d d _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d d d d _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d d _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d d _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d d d _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d d d _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d d d _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d d d _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d d d d _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d d d d _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d d d d _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d d d d _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d d d _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d d d _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d d _ _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ d d d d d d d d d d d d d d d d d _ _ _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ d d d d d d d d d d d d d d d _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ d d d d d d d d d d d d d _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ d d d d d d d d d d d _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ d d d d d d d _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ },
{ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ },
}},
};
int main(int argc, char **argv){
// prevent optimizer from ignoring the big array
std::cout << STRUCTURES.size() << std::endl;
return 0;
}
请注意,我仍然使用单个 std::vector 作为最外层维度。这在编译时不会产生明显的开销。
不使用动态分配有帮助:
template <typename T, int a, int b, int c>
using sprites = std::array<std::array<std::array<T, c>, b>, a>;
但完全不分配肯定更快。现在我不了解你,但我发现初始化程序正确并且排列非常乏味 (TM) 所以,让我们不要:
// an array holding some game assets
static constexpr char STRUCTURES[] {
// pos 0
"_____________________________________"
"_____________________________________"
"_____________________________________"
"_____________________________________"
"_________________________ddddddd_____"
"______________________ddddddddddd____"
"_____________________ddddddddddddd___"
"____________________dddddddddddddd___"
"___________________ddddddddddddddd___"
"__________________dddddddddddddddd___"
"_________________dddddddddddddddd____"
"________________ddddddddddddddddd____"
"_______________ddddddddddddddddd_____"
"______________ddddddddddddddddd______"
"____________dddddddddddddddddd_______"
"___________dddddddddddddddddd________"
"__________ddddddddddddddddddd________"
"_________ddddddddddddddddddd_________"
"________ddddddddddddddddddd__________"
"________ddddddddddddddddddd__________"
"_______dddddddddddddddddddd__________"
"______dddddddddddddddddddd___________"
"______dddddddddddddddddddd___________"
"______dddddddddddddddddddd___________"
"______ddddddddddddddddddd____________"
"______ddddddddddddddddddd____________"
"______dddddddddddddddddd_____________"
"______ddddddddddddddddd______________"
"_______ddddddddddddddd_______________"
"________ddddddddddddd________________"
"_________ddddddddddd_________________"
"___________ddddddd___________________"
"_____________________________________"
"_____________________________________"
"_____________________________________"
"_____________________________________"
"_____________________________________"
// pos 1
// ....
// ....
// ....
"_____________________________________"
"_____________________________________"
// pos 81
"_____________________________________"
"_____________________________________"
"_____________________________________"
"_____________________________________"
"_________________________ddddddd_____"
"______________________ddddddddddd____"
"_____________________ddddddddddddd___"
"____________________dddddddddddddd___"
"___________________ddddddddddddddd___"
"__________________dddddddddddddddd___"
"_________________dddddddddddddddd____"
"________________ddddddddddddddddd____"
"_______________ddddddddddddddddd_____"
"______________ddddddddddddddddd______"
"____________dddddddddddddddddd_______"
"___________dddddddddddddddddd________"
"__________ddddddddddddddddddd________"
"_________ddddddddddddddddddd_________"
"________ddddddddddddddddddd__________"
"________ddddddddddddddddddd__________"
"_______dddddddddddddddddddd__________"
"______dddddddddddddddddddd___________"
"______dddddddddddddddddddd___________"
"______dddddddddddddddddddd___________"
"______ddddddddddddddddddd____________"
"______ddddddddddddddddddd____________"
"______dddddddddddddddddd_____________"
"______ddddddddddddddddd______________"
"_______ddddddddddddddd_______________"
"________ddddddddddddd________________"
"_________ddddddddddd_________________"
"___________ddddddd___________________"
"_____________________________________"
"_____________________________________"
"_____________________________________"
"_____________________________________"
"_____________________________________"
};
朴素的索引助手
现在,当然,您需要整数并将字符映射到某些值(例如本例中的 3 和 999)。为此,我们可以使用一个简单的索引助手:
struct Helper {
constexpr int operator()(int a, int b, int c) const {
switch (auto ch = STRUCTURES[a*37*37 + b*37 + c]) {
case 'd': return 999;
case '_': return 3;
}
return 0;
}
};
现在我们可以编写 main
来打印随机“字形”、“精灵”或任何这些资源:
#include <iostream>
#include <iomanip>
int main() {
auto a = rand() % 81;
Helper index;
for (int b = 0; b < 37; ++b) {
for (int c = 0; c < 37; ++c)
std::cout << std::setw(4) << index(a, b, c);
std::cout << "\n";
}
}
它打印(我有 81 倍相同的“图像”):
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 999 999 999 999 999 999 999 3 3 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 999 999 999 999 999 999 999 999 999 999 999 3 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 999 999 999 999 999 999 999 999 999 999 999 999 999 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 999 999 999 999 999 999 999 999 999 999 999 999 999 999 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 3 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 3 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 3 3 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 3 3 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 3 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 3 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 3 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 3 3 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 3 3 3 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 999 999 999 999 999 999 999 999 999 999 999 999 999 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 999 999 999 999 999 999 999 999 999 999 999 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 3 3 999 999 999 999 999 999 999 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
非朴素索引助手:Boost MultiArray
更少的代码,更多的功能。现在你可以使用 .size()
和 ranged-for:
#include <boost/multi_array.hpp>
int main() {
constexpr std::array shape {81,37,37};
boost::const_multi_array_ref<char, 3> view(STRUCTURES, shape);
auto const& sprite = view[rand() % view.size()];
for (auto const& row : sprite) {
for (auto const& cell : row)
printf("%4d", (cell=='d'? 999 : 3));
putchar('\n');
}
}
现场完整演示
朴素索引
Helper
版本:Live On Coliru编译:(在 coliru 上!)
real 0m0.562s user 0m0.432s sys 0m0.124s
运行时(在 coliru 上!):
real 0m0.005s user 0m0.000s sys 0m0.004s
提升多阵列版本:Compiler Explorer/Coliru
我的系统(两种方法)上的可执行文件大小为 118k (GCC) 或 119k (clang)。