Advertisement
Guest User

Untitled

a guest
Apr 4th, 2012
583
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <sys/time.h>
  3. #include <sys/mman.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6.  
  7. double mydifftime(struct timeval *tv1, struct timeval *tv2)
  8. {
  9.     return ((tv1->tv_sec - tv2->tv_sec) * 1000000.0 +
  10.         (tv1->tv_usec - tv2->tv_usec)) / 1000000.0;
  11. }
  12.  
  13. int main()
  14. {
  15.     int MMAP_SIZE = 1<<20;
  16.     int i;
  17.     printf("   size  \tmmap time\tmunmap time\tt1/t2   \tt2/size\n");
  18.  
  19.     for (i = 0; i < 11; i++, MMAP_SIZE <<= 1) {
  20.  
  21.         struct timeval tv1, tv2;
  22.         double t1, t2;
  23.  
  24.         gettimeofday(&tv1, NULL);
  25.  
  26.         void *addr = mmap(NULL, MMAP_SIZE, PROT_READ | PROT_WRITE,
  27.                   MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  28.         gettimeofday(&tv2, NULL);
  29.  
  30.         if (addr == MAP_FAILED)
  31.             abort();
  32.         t1 = mydifftime(&tv2, &tv1);
  33.  
  34.         /* commit memory */
  35.         memset(addr, 1, MMAP_SIZE);
  36.  
  37.         gettimeofday(&tv1, NULL);
  38.         munmap(addr, MMAP_SIZE);
  39.         gettimeofday(&tv2, NULL);
  40.         t2 = mydifftime(&tv2, &tv1);
  41.  
  42.         printf("%*d\t%f\t%f\t%f\t%f\n", 10, MMAP_SIZE, t1, t2, t2/t1,
  43.                t2*100000000.0/(MMAP_SIZE));
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement