malloc vs mmap in C

前端 未结 6 881
广开言路
广开言路 2020-12-22 23:36

I built two programs, one using malloc and other one using mmap. The execution time using mmap is much less than using malloc

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-23 00:13

    I assume that you are referring to using mmap and malloc for reading data from files. In that case you pretty much got the main point:

    • using fread/fwrite you have to make many calls to the OS.
    • using mmap you appear to get access to the entire file in one operation. This is not entirely true because the OS probably maps the file one memory page at a time, but it is still a lot faster.

提交回复
热议问题