I'm trying to read a 250K line file, and apply regex to each of these lines. However the code is much much slower than Java's readline function. In Java all the parsing is done in ~10 sec, while in C++ it takes more than 2 mins. I've seen the relative C++ ifstream.getline() significantly slower than Java's BufferedReader.readLine()? and added these two lines on top of main:
std::ifstream::sync_with_stdio(false);
std::ios::sync_with_stdio(false);
The rest of the code (I simplified it to remove any delays regex might be causing):
#include "stdafx.h"
#include <ios>
#include <string>
#include <fstream>
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
std::string libraryFile = "H:\\library.txt";
std::ios::sync_with_stdio(false);
std::string line;
int i = 1;
std::ifstream file(libraryFile);
while (std::getline (file, line)) {
std::cout << "\rStored " << i++ << " lines.";
}
return 0;
}
The example seems quite simple, but even the fix suggested in most posts doesn't seem to work. I've run the .exe multiple times using release settings in VS2012, but I just can't reach Java's times.