【发布时间】:2013-03-03 11:38:07
【问题描述】:
对于用 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",
下到
"last line."};
但是当我用 g++ 编译它时,它需要很长时间,以至于我没有看到它完成。它还使用大约 2 GB 的虚拟内存。所以我把strdataB[]注释掉了,然后程序确实编译了,但是过了很久还是这样。可执行文件只有大约 8 Mb 并且运行良好。
我可以做些什么来加快编译过程?我不介意是否必须更改代码,但我不想使用外部文件来加载。我想要一个数组,因为它在程序中非常适合我。
我在网上某处读到“静态常量”应该可以解决问题,但我从经验中了解到它没有。
非常感谢您的任何建议!
【问题讨论】:
-
你在说几行?您的源代码中真的有 30,000 和 60,000 个字符串吗?
-
我假设它们在源文件而不是头文件中。
-
您意识到加载器必须执行与从外部文件加载字符串基本相同的操作?
-
std::string数组和char*数组之间的时间比较是 5 分钟对 2 秒。对象大小从大约 5 MB 下降到仅 1 MB。 -
另外,将文字移动到标题之外的单个翻译单元。在标题中只提供
extern声明,这将降低单个翻译单元(而不是标题的所有用户)的成本
标签: c++ arrays string static constants