Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ (gcc/g++) Two huge arrays of strings take extremely long to compile

For a program written in C++, I need two huge arrays of strings that contain data.

They are defined in a header file as follows:

#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",

down to

    "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."};

But when I compile this with g++, it takes so long that I have not seen it complete. It also uses about two GB of virtual memory. So I commented out strdataB[], and then the program did compile, but still after a long while. The executable was only about 8 Mb and worked fine.

What can I do in order to speed up the compiling process? I don't mind if I have to change the code, but I don't want to use an external file to load from. I would like an array because it works extremely well for me inside the program.

I read on the net somewhere that "static const" should do the trick, but I learned by experience that it doesn't.

Thanks a lot in advance for any suggestions!

like image 265
L.A. Rabida Avatar asked Mar 14 '13 21:03

L.A. Rabida


1 Answers

You should not use std::string for that. Use instead plain old const char*:

const char * const strdataA[30000] = {
    "one",
    "two",
    //...
};

The static keyword shouldn't make much of a difference here.

This way, the strings themselves will be stored in the read-only data section as simple literals, and the array itself will be simply an array of pointers. Also, you avoid running the strings constructors/destructors at runtime.

like image 159
rodrigo Avatar answered Oct 22 '22 15:10

rodrigo