Why is reading lines from stdin much slower in C++ than Python?

前端 未结 10 1987
野趣味
野趣味 2020-11-22 03:06

I wanted to compare reading lines of string input from stdin using Python and C++ and was shocked to see my C++ code run an order of magnitude slower than the equivalent Pyt

10条回答
  •  春和景丽
    2020-11-22 03:42

    By the way, the reason the line count for the C++ version is one greater than the count for the Python version is that the eof flag only gets set when an attempt is made to read beyond eof. So the correct loop would be:

    while (cin) {
        getline(cin, input_line);
    
        if (!cin.eof())
            line_count++;
    };
    

提交回复
热议问题