mmap versus read in MacOS X

Eric Scharff Eric.Scharff at Colorado.EDU
Wed Jan 2 17:25:56 UTC 2002


The read time for loading the image into memory seemed a little high to
me, so I wrote a tiny benchmark.

The simple program attached can be used to compare reading performance of
block reads using read(2) versus mmap(2).  It counts the number of A's
in the file specified on the command line.

On my G3/400 running MacOS 10.1.2, read and mmap performance seem roughly
the same.  On a 75MB file, this took between 2.2 and 2.7 seconds to run.
(Note that my tests include rebooting to avoid cache hits.)

The interesting thing is that the although the read solution takes spends
far more time making system calls (1200ms versus 260ms), the clock time is
roughly the same.

Note too, that on my G3/400, reading a 75MB file takes only 1280ms.  How
does this compare to Squeak's image reading time?

-Eric
-------------- next part --------------
#include <sys/types.h>
#include <sys/mman.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>

/* To use mmap instead of read(2), comment out the next line: */
#define USE_READ


#define READ_SIZE 1024

int main(int argc, char *argv[]) {
  int fileLen;
  int file;
  int i;
  int aCount;
  char buf[READ_SIZE];
#ifndef USE_READ
  char *mmapbuf;
  int len;
#endif

  file = open(argv[1], O_RDONLY);
  if (file < 0) {
    fprintf(stderr, "Error opening %s\n", argv[1]);
    exit(1);
  }
  aCount = 0;
#ifdef USE_READ
  while ((i = read(file, buf, READ_SIZE)) > 0) {
    for (i=i-1; i >= 0; i--) {
      if (buf[i] == 'A') aCount++;
    }
  }
#else
  len = lseek(file, 0, SEEK_END);
  lseek(file, 0, SEEK_SET);
  mmapbuf = mmap((caddr_t) 0, len, PROT_READ, 0, file, 0);
  if (mmapbuf == MAP_FAILED) { perror("mmap failed"); exit(1); }
  for (i=0; i < len; i++) {
    if (mmapbuf[i] == 'A') aCount++;
  }
#endif
  printf("Number of As in %s is %d.\n", argv[1], aCount);
  return 0;
}


More information about the Squeak-dev mailing list