"Leigh Johnston" <leigh@i42.co.uk> wrote in message
news:CfWdneWIYtgvomDWnZ2dnUVZ7qWdnZ2d@giganews.com...
"Peter Olcott" <NoSpam@OCR4Screen.com> wrote in message
news:0PudnRTgeKDRrGDWnZ2dnUVZ_q6dnZ2d@giganews.com...
On 5/26/2010 9:00 AM, Victor Bazarov wrote:
On 5/26/2010 9:54 AM, Peter Olcott wrote:
I need to know the syntax for getting a line of text from a
std::string
buffer. Is there a way to do this besides manually?
std::string FileBuffer; // Holds the entire contents of the file.
std::string LineOfInput; // Holds one "\n" delimited line of text.
You can define an istringstream based on your input (FileBuffer) and
then use 'getline' with that stream. Rinse and repeat.
V
I already know how to do this with a file. The problem is that this
is at least fifty-fold slower than necessary. I want to read the
whole contents of the file, and then do a getline() on this buffer.
Sounds like you are doing premature optimization again. If your file
I/O is buffered then I would not expect much difference performance
wise between using getline on an istringstream and using getline on an
ifstream, I certainly find a "fifty-fold" increase in performance hard
to believe. As you haven't implemented it yet (you are asking how to
do it here) how do know there is such an increase in performance?
Steps:
1) Make it work
2) Profile
3) Make it fast
/Leigh
I did a quick test using a 3.5 MB file (100,000 lines) using the following:
int main()
{
{
timer t("ifstream: ");
std::ifstream file("c:\\tmp\\test.txt");
std::string line;
while(std::getline(file, line));
}
{
timer t("istringstream: ");
std::ifstream file("c:\\tmp\\test.txt");
file.seekg(0, std::ios::end);
std::ifstream::pos_type count = file.tellg();
file.seekg(0, std::ios::beg);
std::string fileContents;
fileContents.resize(count);
file.read(&fileContents[0], count);
fileContents.resize(file.gcount());
std::string line;
std::istringstream iss(fileContents);
while(std::getline(iss, line));
}
return 0;
}
Which outputs:
ifstream: 0.2454 seconds
istringstream: 0.2432 seconds
which implies no real difference in performance between the two methods
like I predicted (certainly not a 50x performance difference). Also
performing getline on an ifstream directly has lower RAM requirements.
/Leigh