Като гледам кода на С++ и Пърл е нормално на С++ да е по-бавен, цикълът на Пърл не борави със стрингове при всяка итерация, а именно:
GeSHi (Perl):
open(MYFILE
, "< :encoding(UTF-8)", "dic.txt");
while(<MYFILE>){
my($line) = $_;
if($duma){
if($line =~ m/($duma)$/i){
#$buffer->insert ($buffer->get_end_iter, "$line\n");
$line1 .= "$line\n";
}
elsif(length($duma)-1>=2 && $line =~ m/($duma1)$/i){ $line2 .= "$line\n";
...
докато С++ цикълът "пере" здраво за всеки ред и всяка отделна под-дума, което забавя драстично целият процес
GeSHi (C++):
wxString filename = _T("dic.txt");
wxFileInputStream fsIn(filename);
if ( !fsIn.Ok() )
{
wxPuts(_T("ERROR: couldn't open file."));
}
else
{
wxTextInputStream tis(fsIn);
for ( ;; )
{
const wxString s = tis.ReadLine();
// line could be non empty if the last line of the file isn't
// terminated with EOL
if ( fsIn.Eof() && s.empty() )
break;
if(duma){
if(wxRegEx (duma+"$").Matches(s)){
list1 += s+"\n";
}
else if((dsize-1 >= 2) && wxRegEx (duma.substr(1)+"$").Matches(s)){
list2 += s+"\n";
}
else if((dsize-2 >= 2) && wxRegEx (duma.substr(2)+"$").Matches(s)){
list3 += s+"\n";
}
...
С++ дори много бързо извършва огромния ти цикъл, Пърл ще е съществено по-бавен при равноправни условия. Имам следните препоръки, кода си твой ти решаваш как ще изглежда, зареждай файла само веднъж в конструктора и използвай вектор от стрингове, като после само ще обикаляш вектора и ще "мачваш". В Project1Dlg.h
GeSHi (C++):
#include <vector>
...
private:
void OnClose(wxCloseEvent& event);
void CreateGUIControls();
std::vector<wxString>dict;
};
после в конструктора го попълваш Project1Dlg.cpp
GeSHi (C++):
Project1Dlg::Project1Dlg(wxWindow *parent, wxWindowID id, const wxString &title, const wxPoint &position, const wxSize& size, long style)
: wxDialog(parent, id, title, position, size, wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
{
CreateGUIControls();
wxString filename = _T("dic.txt");
wxFileInputStream fsIn(filename);
if ( !fsIn.Ok() )
{
wxPuts(_T("ERROR: couldn't open file."));
}
else
{
wxTextInputStream tis(fsIn);
for ( ;; )
{
const wxString s = tis.ReadLine();
// line could be non empty if the last line of the file isn't
// terminated with EOL
if ( fsIn.Eof() && s.empty() )
break;
if(!s.empty())
dict.push_back(s);
}
}
}
и при всяко натискане на бутон всичко ще става за по-малко от секунда, като обхождаш само вектора и търсиш за съвпадение
GeSHi (C++):
std::vector<wxString>::iterator it;
for (it = dict.begin(); it != dict.end(); it++ )
{
const wxString s = (*it);
if(duma){
if(wxRegEx (duma+"$").Matches(s)){
list1 += s+"\n";
}
else if((dsize-1 >= 2) && wxRegEx (duma.substr(1)+"$").Matches(s)){
list2 += s+"\n";
}
...