I've been testing out performance with reading files in C++ using Visual Studio, and I've got some results that I really don't understand.
My code is as follows:
std::vector<unsigned char> Method1(std::string filePath)
{
std::basic_ifstream<unsigned char> file(filePath, std::ios::binary);
file.seekg(0, std::ios::end);
std::streamsize size = file.tellg();
file.seekg(0, std::ios::beg);
std::vector<unsigned char> buffer(size);
file.read(buffer.data(), size);
return buffer;
}
(I actually used uint8_t instead of unsigned char, but since it's just a typedef I've used the latter here to better demonstrate the problem)
I gave it a 3mb file to read and used the std::chrono functions to time it and these were my results:
Debug Configuration - 10.2 seconds
Release Configuration - 98 ms
The big difference between debug and release was already cause for concern.
So I tried replacing all references to "unsigned char" with "char" (std::basic_ifstream<char> instead of std::basic_ifstream<unsigned char> etc.) and re-ran the program.
I found that it ran in under 3ms in both debug and release.
After a bit more fiddling, I realised that the basic_ifstream<unsigned char> was the problem. If I left everything else as is and changed the file to basic_ifstream<char> (with a reinterpret_cast<char *>(buffer.data()) as well), then it also worked fine.
I've verified this on two separate machines, one running the newest release version of Visual Studio 2015 and one running Preview 3 of Visual Studio 15.
Is there some sort of subtlety that makes the basic_ifstream perform so poorly when it's using unsigned chars?