[지식/팁] Which is fastest: read, fread, ifstream or mmap?

Which is fastest: read, fread, ifstream or mmap?

If you program in C/C++, you have many options to read files:

  • The standard C library offers a low-level read function. It is as simple as it gets.
  • The standard C library also offers a higher level fread function. Unlike the read function, you can set a buffer size. Buffers can be good or bad. On the one hand, they reduce the number of disk accesses. On the other hand, they introduce an intermediate step between the disk and you data. That is, they may cause the data to be copied needlessly. Buffers usually makes software faster because copying data in memory is much faster than reading it from disk.
  • In C++, you have ifstream. It is very similar to fread, but with a more object-oriented flavor.
  • Finally, you can use memory mapping. Instead of reading blocks of data, you map the content of the file to a pointer and the operating system is responsible with filling in the data. It has the reputation to be very fast because the data on disk can be mapped directly to memory without any undue copying. However, in my experience, it is also less stable: you are unlikely to cause a bus error with fread or ifstream, but the slightest mistake with memory mapping and your program can crash.

For my work, a lot of the IO is based on sequential access. For this kind of access pattern, I have never found memory mapping to be useful. To support my claim, I created a little program that reads arrays of integers from a file, and does some minor computations on them. Memory mapping is not beneficial:

method millions of int. per s 
C read70
C fread124
C++ ifstream124
mmap125

As usual, my benchmark code is available for inspection. I used a Linux desktop with an Intel Core i7 processor and GCC 4.7 with the -O3 flag for my tests.

Conclusion: For sequential access, both fread and ifstream are equally fast. Unbuffered IO (read) is slower, as expected. Memory mapping is not beneficial.

Warning: Benchmarking IO reliably is difficult. Results will vary depending on your configuration.

0
0
이 글을 페이스북으로 퍼가기 이 글을 트위터로 퍼가기 이 글을 카카오스토리로 퍼가기 이 글을 밴드로 퍼가기

메이커 게시판

번호 제목 글쓴이 날짜 조회수
33 자유 게시판 뿌리를 찾아서 - 고려 호족 금성 12-19 14,051
32 알림/이벤트 2021 주차 달력 (만들어 뵜습니다) icon 청계천도사 12-17 18,415
31 자유 게시판 뿌리를 찾아서 - 남제 금성 12-16 14,979
30 자유 게시판 뿌리를 찾아서 - 연수와 제수 금성 11-24 14,545
29 자유 게시판 뿌리를 찾아서 +1 금성 11-17 14,414
28 자유 게시판 모기 icon 양재동메이커 08-17 13,210
27 지식/팁 Arduino UNO R3 vs ESP32 Infrared Remote Sensor Value icon 양재동메이커 07-09 15,045
26 지식/팁 MG996R 서버 모터 이상 동작 icon 양재동메이커 07-02 15,095
25 자유 게시판 외환 위기와 내환 위기 icon HellMaker 03-12 14,851
24 자유 게시판 타다는 왜 괴롭힘을 당하는가 ? icon HellMaker 03-04 15,174
23 자유 게시판 코로나 19와 세계의 공장 중국 icon HellMaker 03-04 15,568
22 지식/팁 Which is fastest: read, fread, ifstream or mmap? icon 양재동메이커 02-05 15,924
21 자유 게시판 안녕하세요 +1 코끼린 12-04 14,200
20 자유 게시판 2019 메이커 페어 코일 건 출품 icon 양재동메이커 10-21 15,035
19 자유 게시판 에라스무스에게 배우는 22가지 교훈 icon HellMaker 09-06 13,833
18 자유 게시판 안녕하세요? +1 DreamMaker 07-21 13,524
17 자유 게시판 연금술사들의 불행이 가져다준 귀중한 선물 icon 양재동메이커 04-05 16,798
16 지식/팁 Serial Port Plotter icon 양재동메이커 03-09 17,895
15 자유 게시판 [푼글] 행복 쫓아가지말고 그냥 열씨미.. 삽시다. icon 청계천도사 03-01 15,688
14 자유 게시판 가입인사 드립니다. +1 야율초재 02-25 15,662