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

前端 未结 10 2005
野趣味
野趣味 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:45

    getline, stream operators, scanf, can be convenient if you don't care about file loading time or if you are loading small text files. But, if the performance is something you care about, you should really just buffer the entire file into memory (assuming it will fit).

    Here's an example:

    //open file in binary mode
    std::fstream file( filename, std::ios::in|::std::ios::binary );
    if( !file ) return NULL;
    
    //read the size...
    file.seekg(0, std::ios::end);
    size_t length = (size_t)file.tellg();
    file.seekg(0, std::ios::beg);
    
    //read into memory buffer, then close it.
    char *filebuf = new char[length+1];
    file.read(filebuf, length);
    filebuf[length] = '\0'; //make it null-terminated
    file.close();
    

    If you want, you can wrap a stream around that buffer for more convenient access like this:

    std::istrstream header(&filebuf[0], length);
    

    Also, if you are in control of the file, consider using a flat binary data format instead of text. It's more reliable to read and write because you don't have to deal with all the ambiguities of whitespace. It's also smaller and much faster to parse.

提交回复
热议问题