Chinaunix首页 | 论坛 | 博客
  • 博客访问: 984167
  • 博文数量: 96
  • 博客积分: 1553
  • 博客等级: 上尉
  • 技术积分: 1871
  • 用 户 组: 普通用户
  • 注册时间: 2011-12-25 14:50
个人简介

专注点,细心点,耐心点 知行合一

文章分类

全部博文(96)

文章存档

2018年(1)

2014年(4)

2013年(31)

2012年(56)

2011年(4)

分类: C/C++

2013-06-12 14:39:57

点击(此处)折叠或打开

  1. #include <sys/mman.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <fcntl.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <string.h>


  8. #define TIMER(val) do{
  9.         struct timeval tm;
  10.         gettimeofday(&tm, NULL);
  11.         val = tm.tv_sec * 1000 + tm.tv_usec/1000;
  12. }
  13. while(0)

  14. int i = 0;

  15. int create_tmp_file()
  16. {
  17.         char dir[100] = "/tmp/mmap";
  18.         char file[100] = {0};
  19.         snprintf(file, 100, "%s/%d", dir, i);
  20.         i++;

  21.         int fd = open(file, O_RDWR | O_CREAT);
  22.         if(fd<0)
  23.         {
  24.                 printf("errorn");
  25.                 exit(1);
  26.         }
  27.         return fd;
  28. }

  29. int create_tmp_file2()
  30. {
  31.         char dir[100] = "/tmp/mmap";
  32.         char file[100] = {0};
  33.         snprintf(file, 100, "%s/%d", dir, i);
  34.         i++;

  35.         int fd = open(file, O_CREAT | O_RDWR | O_APPEND);
  36.         if(fd<0)
  37.         {
  38.                 printf("errorn");
  39.                 exit(1);
  40.         }
  41.         return fd;
  42. }

  43. char *mmap_fd(int fd, int size)
  44. {
  45.         if(lseek(fd, size-1, SEEK_SET)==-1)
  46.                 printf("lseek errorn");
  47.         if(write(fd, "0", 1)!=1)
  48.                 printf("write errorn");
  49.         //if(fsync(fd)==-1)
  50.         // printf("fsync errorn");
  51.         char *p = (char *) mmap(NULL, size, PROT_WRITE, MAP_SHARED, fd, 0);
  52.         if(p==MAP_FAILED)
  53.         {
  54.                 perror("mmap");
  55.                 exit(1);
  56.         }
  57.         return p;
  58. }

  59. int main()
  60. {
  61.         long starttime, endtime;
  62.         starttime = endtime = 0;

  63.         int size = 1024 * 5;
  64.         int count = 1000;
  65.         char test_string[1024] = "lkjklsdjffffffffffffffffffffffffffffaelkjklgjiowejklgjkljaskldj";
  66.         int test_string_len = strlen(test_string);

  67.         //1. test mmap
  68.         TIMER(starttime);
  69.         int fd = create_tmp_file();
  70.         char *p = mmap_fd(fd, size);
  71.         char *tmp_p = p;

  72.         int j =0;
  73.         int size_sum = 0;

  74.         for(; j<count; j++)
  75.         {
  76.                 size_sum += test_string_len;
  77.                 if(size_sum > size)
  78.                 {
  79.                         close(fd);
  80.                         munmap(p, size);
  81.                         fd = create_tmp_file();
  82.                         p = mmap_fd(fd, size);
  83.                         size_sum = test_string_len;
  84.                         tmp_p = p;
  85.                 }

  86.                 memcpy(tmp_p, test_string, test_string_len);
  87.                 tmp_p += test_string_len;
  88.         }
  89.         close(fd);
  90.         munmap(p, size);
  91.         TIMER(endtime);
  92.         printf("mmap duration: %ldn", endtime - starttime);

  93.         //2. write
  94.         size_sum = 0;
  95.         TIMER(starttime);
  96.         fd = create_tmp_file2();
  97.         for(j=0; j<count; j++)
  98.         {
  99.                 size_sum += test_string_len;
  100.                 if(size_sum > size)
  101.                 {
  102.                         close(fd);
  103.                         fd = create_tmp_file2();
  104.                         size_sum = test_string_len;
  105.                 }
  106.                 write(fd, test_string, test_string_len);
  107.         }
  108.         close(fd);
  109.         TIMER(endtime);
  110.         printf("batch write duration: %ldn", endtime - starttime);

  111.         //3. write
  112.         TIMER(starttime);
  113.         for(j=0; j<count; j++)
  114.         {
  115.                 fd = create_tmp_file();
  116.                 write(fd, test_string, test_string_len);
  117.                 close(fd);
  118.         }
  119.         TIMER(endtime);
  120.         printf("loop write duration: %ldn", endtime - starttime);

  121.         //4. buffer write
  122.         size_sum = 0;
  123.         TIMER(starttime);
  124.         int index = 0;
  125.         char tmp_buffer[size];
  126.         fd = create_tmp_file2();
  127.         for(j=0; j<count; j++)
  128.         {
  129.                 size_sum += test_string_len;
  130.                 if(size_sum > size)
  131.                 {
  132.                         write(fd, tmp_buffer, size_sum-test_string_len);
  133.                         close(fd);
  134.                         fd = create_tmp_file2();
  135.                         size_sum = test_string_len;
  136.                 }
  137.            
  138.                 strncpy(tmp_buffer+size_sum-test_string_len, test_string, test_string_len);
  139.         }
  140.         write(fd, tmp_buffer, size_sum-test_string_len);
  141.         close(fd);
  142.         TIMER(endtime);
  143.         printf("buffer write duration: %ldn", endtime - starttime);

  144.         return 0;
  145. }
gcc mmap-test.c -o mmaptest
./mmaptest

result:
    mmap duration: 3
    batch write duration: 8
    loop write duration: 77
    buffer write duration: 3
阅读(3426) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~