How can I improve this?
I have the follow function that find a substr in a file. Mainly to perform lookups without loading file into memory.
It works. I was just wondering what I could do to make it faster. Not really sure what to do. On files over about 3000kb you can notice the lookup. Files over 30mb it takes forever(30 seconds approx.)
typedef std::pair<long,long> FindData;
//Find position of requested substr
FindData FindSubstr(const std::string& filename, const std::string& str)
//Temporary xData structure to return
std::cout << "\n\nInitiating filestream for FindSubstr().\n";
if ( !load.good() )
std::cout << "-->Could not open filestream. Aborting...\n\n";
std::cout << "\nSearching file for substr. Please Wait...\n";
size_t bytes = 0;
while ( load.get(byte) )
if ( Mem.length() >= str.length() )
Mem += byte;
//Check for match
if ( Mem == str )
std::cout << "-->Match found after " << bytes << " bytes.\n\n";
Temp.first = bytes-Mem.length();
Temp.second = bytes;
std::cout << "-->No match found.\n\n";
Thanks for any help or ideas.
Obviously, it's very inefficient to read and load one byte at a time.
I have to admit that the limited buffering requirement is a bit tedious but there are faster algorithms out there. You could try implementing this one or several others, depending on how this will be used.