对于一个用C++编写的程序,我需要两个包含数据的大型字符串数组。
它们在头文件中定义如下:
#include <string>
static const string strdataA[30000]={"this is the first line of the data",
"the second line of data",
"other stuff in the third line",
降至
"last line."};
//second array strings
static const string strdataB[60000]={"this is the first line of the data",
"the second line of data",
"other stuff in the third line",
down to
"last line."};
但当我用g++编译它时,它需要的时间非常长,以至于我没有看到它完成。 它还使用了大约2 GB的虚拟内存。 所以我注释掉了strdataB [],然后程序确实编译了,但仍然需要很长时间。可执行文件只有约8 MB,并且可以正常工作。
为了加快编译速度,我该怎么办? 如果必须更改代码,我也不介意,但是我不想使用外部文件进行加载。 我希望使用数组,因为它在程序中非常有效。
在网上读过“静态常量”应该起到作用,但我通过经验学到它并不起作用。
感谢您提前给出任何建议!
std::string
数组和char*
数组的时间比较为 5 分钟 vs 2 秒。对象大小从约 5 MB 减少到仅 1 MB。 - Olaf Dietscheextern
声明即可,这将减少成本到一个翻译单元(而不是头文件的所有用户)。 - David Rodríguez - dribeas