Why are std::fstreams so slow?

前端 未结 5 861
不思量自难忘°
不思量自难忘° 2021-01-30 20:49

I was working on a simple parser and when profiling I observed the bottleneck is in... file read! I extracted very simple test to compare the performance of fstreams

5条回答
  •  执念已碎
    2021-01-30 21:22

    The stream is somehow broken on the MAC, old implementation or setup.

    An old setup could cause the FILE to be written in the exe directory and the stream in the user directory, this shouldn't make any difference unless you got 2 disks or other different setting.

    On my lousy Vista I get Normal buffer+Uncached:
    C++ 201103
    FILE* write 4756 ms
    FILE* read 5007 ms
    fstream write 5526 ms
    fstream read 5728 ms

    Normal buffer+Cached:
    C++ 201103
    FILE* write 4747 ms
    FILE* read 454 ms
    fstream write 5490 ms
    fstream read 396 ms

    Large Buffer+cached:
    C++ 201103
    5th run:
    FILE* write 4760 ms
    FILE* read 446 ms
    fstream write 5278 ms
    fstream read 369 ms

    This shows that the FILE write is faster than the fstream, but slower in read than fstream ... but all numbers are within ~10% of each other.

    Try adding some more buffering to your stream to see if that helps.

    const int MySize = 1024*1024;
    char MrBuf[MySize];
    stream.rdbuf()->pubsetbuf(MrBuf, MySize);
    

    The equivalent for FILE is

    const int MySize = 1024*1024;
    if (!setvbuf ( file , NULL , _IOFBF , MySize )) 
        DieInDisgrace();
    

提交回复
热议问题