ifstream slow to close
-
Hi, I'm getting some terrible performance when calling ifstream close. It takes about the same amount of time to close() as it takes to read my 64 Mb of data! I've been playing around with it and found it only happens if I have been writing to a file recently. Here's my test code:
void readTest(const std::string & path)
{
ifstream in;
in.open(path.c_str(), ios_base::in | ios_base::binary);
char * buffer = new char[67108864];
in.read(buffer, 67108864);
SW_ELAPSED_MSG()
in.close();
SW_ELAPSED_MSG()
delete [] buffer;
}void writeTest(const std::string & path)
{
ofstream out;
out.open(path.c_str(), ios_base::out | ios_base::binary);
char * buffer = new char[67108864];
out.write(buffer, 67108864);
//out.flush();
out.close();
delete [] buffer;
}int _tmain(int argc, _TCHAR* argv[])
{
writeTest("testfile");
SW_START();
readTest("testfile");
SW_ELAPSED_MSG()
return 0;
}Note my macros do the timing and display it on screen. I don't believe there's any problem with that code. So anyone know why? I'm closing the streams. Why is the read stream affected at all by the write stream? Also as you can see I have a flush() statement in there. It doesn't help, commented or not.