Hello,
Basic c++ question here. I'm trying to read a large file on Windows 7 Pro. C++ compiler is Visual Studio 2010. (ver 16.0). I'm finding that the program runs about 5 times slower on Windows 7 than one on a virtual machine running Ubuntu on the same box. Ubuntu version 10.04 using gcc 4.4.3. The file is rather large ~900MB. The code in question can be narrowed to the following snippet. Any clues on Windows specific tuning to read files faster? The file is about 17 million lines and it takes about 13 seconds on Windows 7 and about 2.3 seconds on Ubuntu (which is a VM on the same Windows 7 box). I'm using /O2 flags on visual c++ and -O3 on Ubuntu/gcc 4.4.3
Thanks
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main(int argc, char *argv[])
{
const char* test_file_path = argv[1];
ifstream ifs(test_file_path);
if (!ifs.is_open()) {
cout << "Could not open " << test_file_path << endl;
return 0;
}
unsigned long line_count = 1;
unsigned long sum = 0;
string line;
// Go through all the lines in the file
while (getline(ifs, line)) {
line_count++;
}
cout << line_count << '\n';
return 0;
}
Edit: Tried boost memory mapped file suggestion by Anders and time dropped to 1.2 seconds. Looks like Ubuntu sort of defaults to this while you need to be explicit on Windows. Thanks Anders.