Thursday, August 23, 2018

android - C++ ifstream.getline() significantly slower than Java's BufferedReader.readLine()?



I'm in the process of rewriting one of my Android applications to take advantage of the NDK and one of the first things it has to do every time is open a 1.5MB text file (approximately 150k lines) and put every line in a data structure. When I did this operation using Java's BufferedReader.readLine(), reading the file from the SD card takes ~2.5 seconds. Here's the code I used for this:



try {
BufferedReader br = new BufferedReader(new FileReader("/sdcard/testfile.txt"));
String thisLine;

while ((thisLine = br.readLine()) != null) {
Log.d(TAG, thisLine);
}
} catch (IOException e) {
//Log error
}


Using C++ with ifstream takes MUCH longer...around 3 minutes for the same file. Here's the code I used in C++:




char buffer[256];
ifstream ifs;
ifs.open("/sdcard/testfile.txt", ifstream::in);
if (ifs.is_open()) {
while (!ifs.eof()) {
ifs.getline (buffer,100);
LOGD(buffer);
}
}



I'm pretty rusty on C++, but can't think of any logical explanation for the increased read time. For a while I thought it might be the LOGD function, but I tried taking that out altogether and the read time wasn't really helped much at all. Does anyone have any ideas on what the issue could be? Is there any faster way to read a file line by line in C++? Thanks.


Answer



One thought is the stdio synchronization might be slowing you down. That can be turned off. I don't know if that would account for all of the difference, but you could try. Also, you're not using eof() correctly. Finally, I'd use the std::string version of getline()



std::ios::sync_with_stdio(false);
ifstream ifs("/sdcard/testfile.txt");
std::string line;
while (getline(ifs, line))
{

LOGD(line);
}


I haven't tested this code, but you can try it and see if it makes a difference.


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...