mmap() vs. reading blocks

前端 未结 12 1533
醉酒成梦
醉酒成梦 2020-11-22 16:59

I\'m working on a program that will be processing files that could potentially be 100GB or more in size. The files contain sets of variable length records. I\'ve got a first

12条回答
  •  礼貌的吻别
    2020-11-22 17:37

    I agree that mmap'd file I/O is going to be faster, but while your benchmarking the code, shouldn't the counter example be somewhat optimized?

    Ben Collins wrote:

    char data[0x1000];
    std::ifstream in("file.bin");
    
    while (in)
    {
        in.read(data, 0x1000);
        // do something with data 
    }
    

    I would suggest also trying:

    char data[0x1000];
    std::ifstream iifle( "file.bin");
    std::istream  in( ifile.rdbuf() );
    
    while( in )
    {
        in.read( data, 0x1000);
        // do something with data
    }
    

    And beyond that, you might also try making the buffer size the same size as one page of virtual memory, in case 0x1000 is not the size of one page of virtual memory on your machine... IMHO mmap'd file I/O still wins, but this should make things closer.

提交回复
热议问题