1

I'm trying to read lines using getline function in gcc on Ubuntu and MSVS on Windows. The time taken to process 200MB text file on Windows is 3 seconds whereas the time taken on Ubuntu is 0.1 second. Both are X64 Release builds.

Not sure why Windows is slower. Any reason for this? Is there any other getline function in Windows that matches the performance of that in Ubuntu?

#include <iostream>
#include <fstream>
#include<string>
using namespace std;

int main()
{
    string line;
    int num = 0;
    ifstream infile;
    infile.open("testdata.txt");
    if (infile.is_open())
    {
        while (!infile.eof()) // To get you all the lines.
        {
            getline(infile, line);
            num++;
        }
    }
    else
        printf("file not found");
    cout << num;
    return 0;
}
user4581301
  • 29,019
  • 5
  • 26
  • 45
  • 3
    https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – Baum mit Augen Sep 09 '16 at 18:38
  • Lot of possibilities here. Different implementations of `getline` from different compilers and different file access and security models jump out immediately. Looking at the second for a moment, have you performed the same experiment with simply opening an performing a binary read of the entire file to see if file reading in general is slower? – user4581301 Sep 09 '16 at 18:43
  • Did you at least compile in release mode for Windows ? – Christophe Sep 09 '16 at 19:40
  • Post says both windows and Linux are x64 release builds. – RedFox Sep 09 '16 at 20:16
  • Most likely the file was already cached on the fast one. You can't test performance unless you use an actual benchmark. – stark Sep 09 '16 at 22:49
  • What makes you think it's the `getline` call and not, say, the `std::string` c'tor? If you need to answer performance questions, use a profiler. Btw., since you didn't explicitly mention it: Are you running the code on the same machine? – IInspectable Sep 10 '16 at 07:07

0 Answers0