[지식/팁] 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
이 글을 페이스북으로 퍼가기 이 글을 트위터로 퍼가기 이 글을 카카오스토리로 퍼가기 이 글을 밴드로 퍼가기

메이커 게시판

번호 제목 글쓴이 날짜 조회수
53 지식/팁 심박수 금성 08-12 14,565
52 지식/팁 vector push_back 오류 icon 양재동메이커 07-27 14,620
51 지식/팁 [ASP] 프로시져 parameters 정리 icon 양재동메이커 07-09 14,837
50 자유 게시판 시발노무색기 (始發奴無色旗) icon 양재동메이커 06-28 16,173
49 지식/팁 코딩 표준(변수 명명법) icon HellMaker 05-06 17,038
48 지식/팁 USB 단자 규격 케이블의 종류 icon HellMaker 05-05 15,625
47 지식/팁 VC ++ 속도 최적화 옵션 icon 양재동메이커 04-24 13,717
46 지식/팁 HC-06 AT 명령어 icon 양재동메이커 04-13 13,541
45 지식/팁 [C/C++ 언어] __DATE__, __TIME__, __FILE__, __LINE__ icon 양재동메이커 04-13 14,664
44 자유 게시판 프로이드 꿈의 해석 icon 양재동메이커 03-04 14,164
43 자유 게시판 부동산 정책과 아파트 가격의 상관 관계 icon 양재동메이커 03-02 15,842
42 자유 게시판 어느새… 외국인 근로자 100만명 icon 양재동메이커 02-19 12,894
41 자유 게시판 닭집 사장님의 수렴의 법칙 +1 icon 양재동메이커 02-19 13,536
40 자유 게시판 일본에는 동사무소가 없다. icon 양재동메이커 02-17 14,274
39 자유 게시판 출퇴근 시간의 교통 혼잡 일부 해결 방안 +1 icon 양재동메이커 02-15 15,429
38 자유 게시판 왜 서양 물건은 왜 싸가지 없을까 ? - 2. 동양과 서양의 구분 icon 양재동메이커 02-10 13,665
37 자유 게시판 왜 서양 물건은 왜 싸가지 없을까 ? - 1. 서두 icon 양재동메이커 02-10 13,884
36 지식/팁 2021 달력 icon 청계천도사 01-19 15,272
35 자유 게시판 뿌리를 찾아서 - 금성 금성 12-25 14,351
34 자유 게시판 뿌리를 찾아서 - 마한 금성 12-20 14,137