I just create a little program for myself to do some tedious work, finding a particular string in a text file.
The program is done, basically it reads the text file line by line, and after reading each line, I have a if statement to check the string against the line. However, it finds it quite slow. If I open the text file in notepad, and using 'Find', the done can be done in a second for a file, but it could take up to minute to be done by my program.
So I would like to discover any alternative and find the most efficient way to do this job please.
if (inFile.is_open())
{
while (getline(inFIle, textline)
{
if (Check_F310_Pass(textline)
F310pass++;
if (Check_F310_Fail(textline)
F310fail++;
if (Check_F313_Pass(textline)
F313pass++;
if (Check_F313_Fail(textline)
F313fail++;
if (Check_F316_Pass(textline)
F316pass++;
if (Check_F316_Fail(textline)
F316fail++;
}
}
however, when I have to do my string search, those Check_xyz, I still have to go through it line by line by get line, don't I? does it mean that I am doing this buffer as an extra work??
This may have more to do with file buffering than with search algorithms. Try changing the code to simply read the file and see how fast it is. If this is too slow then you need a larger buffer.
I have to run, but I think there's a way to give the streambuf more space. Off hand I suggest 64k.
If you can't do it with a streambuf, then try switching to C style I/O. I know that you can specify the buffer that way.