Your profile output suggests that the first area to optimize is the file reading. Specifically:
wchar_t wch[256];
wstring p1, p2;
while (r_dic >> wch >> p1 >> p2) {
pTrie* pt = (*root).insert(splitJamo(wch).c_str(), p1+L' '+p2);
pt->addArche(wch);
}
This reads three strings repeatedly. wch
is read into a character array, but then passed to splitJamo()
which returns a wstring
, which requires memory allocation. That might be a bit slow, but I can't tell because you haven't shown the code for splitJamo()
.
You read p1
and p2
and immediately concatenate them with a space. This is inefficient: they were separated by whitespace in the input file, and you read them separately, allocating memory for them, but then put them back together again.
Assuming the three strings appear on each line of the input file, I'd read it like this:
wchar_t wch[256];
wstring p1p2;
while (r_dic >> wch && std::getline(r_dic, p1p2)) {
pTrie* pt = root->insert(splitJamo(wch), p1p2);
pt->addArche(wch);
}
This reads p1 and p2 together, which should be an improvement. A further improvement might be to use getline()
to read the entire line at once, but we can't tell without seeing the code for splitJamo()
and insert()
.
Also note I removed c_str()
from the first argument to insert()
because I assume it probably takes a wstring
, so we avoid constructing a new one this way. But if it requires wchar_t*
, you can put back the c_str()
.