I need to write a file parser in c++.
Here’s my code :
std::string line;
vector<string> slice;
while(getline(m_inputStream, line))
{
}
My file is big, so this loop takes 12 seconds.
My c# code is :
StreamReader sr = new StreamReader(fileName);
string strline = "";
while (!sr.EndOfStream)
{
strline = sr.ReadLine();
}
And it takes 0.6 seconds… What am I doing wrong in my C++?
firstly, what are you doing with
slice?Chances are the C# version is reading into the string then discarding it – and the c# JIT is optimising that into a no-op, so the 0.6 seconds it takes is just to initialise and quit. The C++ version will generate code to read the string so it really is processing the input file. Make sure the C++ one is built with Release settings if you’re going to compare performance, debug code is meaningless for perf.
Do something with the string and you’ll see different performance figures, and also check your memory usage in both systems, the C# one will use up a lot more RAM until the GC kicks in.