file io 写入性能对比



#include <stdio.h>

#include <stdlib.h>

#include <sys/mman.h>

#include <sys/types.h>

#include <fcntl.h>

#include <unistd.h>

#include <sys/time.h>

#include <time.h>

#include <string.h>

 

#define SIZE 50*1024*1024

int main(int argc, char** argv)

{

         FILE *fp;

         char c = 'a';

         int i;

         char *pmap;

         int fd;

         char *mem;

         struct timeval t1, t2;

         long spend_us;

         printf("write size: %dMB\n", SIZE/1024/1024);

         if((fp = fopen("disk_file", "w")) == NULL)

         {

                   printf("fopen error!\n");

                   return -1;

         }

         gettimeofday(&t1, NULL);

         for(i = 0; i < SIZE; i++)

         {

                   if(fwrite(&c, sizeof(char), 1, fp) != 1)

                            printf("file write error!\n");

         }

         gettimeofday(&t2, NULL);

         spend_us = 1000000 * (t2.tv_sec - t1.tv_sec) + (t2.tv_usec - t1.tv_usec);

         printf("write disk file,spend time:%ld us\n", spend_us);

         fclose(fp);

 

         fd = open("mmap_file", O_RDWR);

         if(fd < 0){

                   perror("open mmap_file");

                   return -1;

         }

         pmap = (char *)mmap(NULL, sizeof(char) * SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);

         if(pmap == MAP_FAILED){

                   perror("mmap");

                   return -1;

         }

         gettimeofday(&t1, NULL);

         for(i = 0; i < SIZE; i++)

         {

                   memcpy(pmap + i, &c, 1);

         }

         gettimeofday(&t2, NULL);

         spend_us = 1000000 * (t2.tv_sec - t1.tv_sec) + (t2.tv_usec - t1.tv_usec);

         printf("write mmap file,spend time:%ld us\n", spend_us);

         close(fd);

         munmap(pmap, sizeof(char) * SIZE);

         mem = (char *)malloc(sizeof(char) * SIZE);

         if(!mem)

         {

                   printf("mem malloc error!\n");

                   return -1;

         }

         gettimeofday(&t1, NULL);

         for(i = 0; i < SIZE; i++)

         {

                   memcpy(mem + i, &c, 1);

         }

         gettimeofday(&t2, NULL);

         spend_us = 1000000 * (t2.tv_sec - t1.tv_sec) + (t2.tv_usec - t1.tv_usec);

         printf("write mem,spend time:%ld us\n", spend_us);

         free(mem);

         return 0;

}

1B写入, 50*1024*1024 次
write size: 50MB
write disk file,spend time:790204 us (磁盘文件,只打开一次)
write mmap file,spend time:118592 us
write mem,spend time:118319 us


write disk file,spend time:2833924027 us (磁盘文件,每次写入打开一次)
write mmap file,spend time:121040 us
write mem,spend time:123624 us

总结: 即可通过只打开一次文件一直读写加速,在其他部分代码耗时和写入数据长度确定的情况下可提速。但实际中其他部分的代码性能可能占比更高超过写文件耗时;这时可能只能通过写内存抓数统一导出的方式来提速。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值