Speeding up file I/O: mmap() vs. read()

前端 未结 3 786
被撕碎了的回忆
被撕碎了的回忆 2020-12-08 02:26

I have a Linux application that reads 150-200 files (4-10GB) in parallel. Each file is read in turn in small, variably sized blocks, typically less than 2K each.

I c

3条回答
  •  一向
    一向 (楼主)
    2020-12-08 02:40

    The problem here doesn't seem to be which api is used. It doesn't matter if you use mmap() or read(), the disc still has to seek to the specified point and read the data (although the os does help to optimize the access).

    mmap() has advantages over read() if you read very small chunks (a couple of bytes) because you don't have call the os for every chunk, which becomes very slow.

    I would also advise like Basile did to read more than 2kb consecutively so the disc doesn't have to seek that often.

提交回复
热议问题