I recently learned about std::string_view and how it is much faster than allocating strings so I am trying to use this in place of std::string where possible.
Is there a way of optimizing a loop that parses a file line by line to use std::string_view instead?
This is the code I am working on.
std::string line;
// loop until we find the cabbage tag
while (std::getline(csd, line))
{
//DO STUFF
if (line.find("</STOP>") != std::string::npos)
break;
}
What you're looking for is mmap
, that allows you to read into a file's data without copying them. Reading from a stream in C++ will always copy the data. You can then, of course, use std::string_view
to point at the data revealed by mmap
, and do all operations you like.