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

메이커 게시판

번호 제목 글쓴이 날짜 조회수
50 자유 게시판 필부(匹夫)가 작금(昨今)의 사태(事態)를 바라보며 한탄(恨歎)을 한다. icon 양재동메이커 12-05 6,321
49 자유 게시판 가입인사 +1 RE호섭 11-29 11,961
48 자유 게시판 대한 민국이 선진국이라 의미에 대하여 icon 양재동메이커 02-07 16,609
47 자유 게시판 평가 없는 정부 와 공기업 +1 icon 양재동메이커 04-11 19,340
46 자유 게시판 눈에 보이지 않는 오류에 대하여 icon HellMaker 04-08 18,386
45 자유 게시판 아파트 vs 치킨 +1 icon 양재동메이커 04-02 18,178
44 자유 게시판 프로그램 언어는 세미콜론 끝나는 언어 그렇치 않은 언어 구분 된다. icon HellMaker 03-17 16,619
43 자유 게시판 잘나가는 기업은 창고 문이 2개 icon 양재동메이커 03-04 16,206
42 자유 게시판 사람이 아닌 시스템으로 돌아 가는 사회 icon 양재동메이커 03-04 16,754
41 자유 게시판 이재명 공약 icon 양재동메이커 03-03 16,448
40 자유 게시판 문재인 대통령의 공약 이행률 icon 양재동메이커 03-02 18,075
39 자유 게시판 여성 가족부 강사료 사례비 지급 기준 +1 icon 양재동메이커 02-17 20,241
38 자유 게시판 대한 민국 3만 달러의 저주와 메이커 icon 양재동메이커 11-20 17,985
37 자유 게시판 저 출산을 해결 해야 되는 문제 일까 ? +1 icon HellMaker 09-17 17,634
36 자유 게시판 북극 항로 개발 icon 양재동메이커 09-15 18,276
35 자유 게시판 왜 좋은 일자리가 없을까 ? icon 양재동메이커 09-15 18,288
34 자유 게시판 창조 경제 vs 소득 주도 성장 개인적인 사견 icon 양재동메이커 09-14 17,762
33 자유 게시판 닭튀김 수렴의 공식 icon 양재동메이커 09-13 18,090
32 자유 게시판 0 0 정당 만들면 어떨까 합니다 !!! icon 양재동메이커 08-20 18,683
31 자유 게시판 시발노무색기 (始發奴無色旗) icon 양재동메이커 06-28 21,423