如何查看fwrite多次写入写入文件会经过几道缓存

当前位置:&&技术问答>
做socket程序,用recv,send这些函数好还是用fread,fwrite好?
&&&&来源:&互联网& 发布时间:
&&&&本文导语:&
是不是recv,send效率低呢?也就是时间长呢?
用read/write,recv/send都行,不要用fread系列。
效率不低。
做socket程序,用recv,send这些函数好还是用fread,fwrite好?
一般说来是...
是不是recv,send效率低呢?也就是时间长呢?
用read/write,recv/send都行,不要用fread系列。
效率不低。
做socket程序,用recv,send这些函数好还是用fread,fwrite好?
一般说来是用recv,send,read,write等函数,用fopen系列函数是在C库中有缓冲的,一般用于对文件进行操作,而用read系列函数在C库中无缓冲,用于设备级读写。
缓冲有几个概念:
一是应用级缓冲,就是在C库中的缓冲,比如在LINUX中的printf中如果不加n它就暂不输出,道理是一样的。
二是块设备高冲缓冲,这是在内核磁盘高速缓冲区的概念
三是硬件缓冲,比如CACHE,外围设备在硬件上的缓冲等,不过不用了解到这个层面上去
不建议使用fread和fwrite不仅考虑效率问题。在很多异构系统通过网络互连起来,在一个系统上写的数据,在另一个系统上读,在这种情况下,这两个函数就不能正常工作,其原因是:
1。在一个结构中,同一成员的位移量可能随编译程序和系统的不同而异(由于不同的对准要求)。在某些编译程序有一选项,允许紧密包装结构(节省存储空间,而运行性能有所下降)或准确对齐,以便在运行时易于存取结构中的各成员,这意味着即使在单一系统上,一个结构的二进制存放方式也可能因编译程序的选择项而不同。
2。用来存储多字节整数和浮点值的二进制格式在不同系统结构间也可能不同。
您可能感兴趣的文章:
本站()旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。本站()站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
相关文章推荐
特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!
(C)2012-,E-mail:www_169it_(请将#改为@)博客访问: 184423
博文数量: 45
博客积分: 157
博客等级: 入伍新兵
技术积分: 493
注册时间:
认证徽章:
IT168企业级官微
微信号:IT168qiye
系统架构师大会
微信号:SACC2013
分类: LINUX
fread返回的是一个FILE结构指针
而read返回的是一个int的文件号
前者fopen/fread的实现是靠调用底层的open/read来实现的.
fopen/fread
是C标准的库函数,操作的对象是: file stream
是和操作系统有关的系统调用。操作的对象是: “file descriptor”
f是ANSI的C标准库。后面的是UNIX下的系统调用。
带f的带有缓冲,是后面的衍生,
直接和硬件打交道,必须是后面的!
环境下的C 对二进制流文件的读写有两套班子:
这里简单的介绍一下他们的区别。
C库函数;open系列是 POSIX 定义的,是UNIX系统里的system call。
fopen系列更具有可移植性;而open系列只能用在 POSIX 的操作系统上。
fopen 系列函数时要定义一个指代文件的对象,被称为“文件句柄”(file handler),是一个结构体;而open系列使用的是一个被称为“文件描述符” (file descriptor)的int型整数。
I/O,读写时使用缓冲;而open系列相对低层,更接近操作系统,读写时没有缓冲。由于能更多地与操作系统打交道,open系列可以访问更改一些fopen系列无法访问的信息,如查看文件的读写权限。这些额外的功能通常因系统而异。
fopen系列函数需要"#include ";使用open系列函数需要"#include " ,链接时要之用libc(-lc)
fopen系列无法实现的功能的情况下,fopen系列是首选。
和fread/fwrite区别
是带缓冲的,read不带缓冲
是标准c里定义的,open是POSIX中定义的可以读一个结构. read在linux/unix中读二进制与普通文件没有区别不能指定要创建文件的权限.open可以指定权限返回指针,open返回文件描述符(整数中任何设备都是文件,都可以用
fread 和fwrite,它自动分配缓存,速度会很快,比自己来做要简单。如果要处理一些特殊的描述符,用read 和write,如套接口,管道之类的
write的效率取决于你buf的大小和你要写入的总数量,如果buf太小,你进入内核空间的次数大增,效率就低下。而fwrite会替你做缓存,减少了实际出现的系统调用,所以效率比较高。
(可能吗?),这俩差不多,严格来说write要快一点点(因为实际上fwrite最后还是用了write做真正的写入文件系统工作),但是这其中的差别无所谓。
阅读(2560) | 评论(0) | 转发(2) |
相关热门文章
给主人留下些什么吧!~~
请登录后评论。本帖子已过去太久远了,不再提供回复功能。测试linux下 fprintf fwrite write mmap 等写文件的速度 - 推酷
测试linux下 fprintf fwrite write mmap 等写文件的速度
/****************************************************************
* 说明: 用不同的方法,测试写文件的速度,测试结果表明
*&&&&&& 改变COUNT的值,将改变写入文件的大小
*&&&&& 测试CPU使用率方法: /usr/bin/time -f &CPU: %P& ./a.out 1
* ************************************************************/
#include &cstdio&
#include &time.h&
#include &iostream&
#include &sys/types.h&
#include &sys/stat.h&
#include &fcntl.h&
#include &sys/mman.h&
#include &unistd.h&
#include &stdlib.h&
#include &string.h&
#include &malloc.h&
#include &sys/time.h&
#define WRITE_FILE&&&&&&& /*定义这个宏,则把数据写到文件,否则不写,用于测试不写文件用的时间*/
const unsigned short usPageSize = 4*1024;&&&&&&&&&&& /*4K*/
const unsigned long STR_LEN = (usPageSize*1024) +1;&&&&& /*每次缓存4M有效数据写入文件 ,最后一个字节保证 null-terminated ,不写入文件*/
const unsigned long COUNT = 256;&&&&&&&& /*改变COUNT的值,将改变写入文件的大小 256 ---&1G& 16 ---& 64M 请问 为什么512 2G出现总线错误?& */
char g_strUserFlow[2048] ;
/****************************************************************
* 功能: 用fprintf,测试写文件速度
* ************************************************************/
void fprintf_write1();
/****************************************************************
* 功能: 用fwrite,(使用系统缓存)测试写文件速度
* ************************************************************/
void fwrite_write2();
/****************************************************************
* 功能: 用fwrite(自己分配4M缓存,每次写入4M, 然后fflush),测试写文件速度
* ************************************************************/
void fwrite_write2_1();
/****************************************************************
* 功能: 用write, 设O_DIRECT,自己维护缓存, ,测试写文件速度
* ************************************************************/
void write_direct3();
/****************************************************************
* 功能: 用write,(使用系统缓存),测试写文件速度
* ************************************************************/
void write_write4();
/****************************************************************
* 功能: 用write,(自己分配4M缓存,每次写入4M, 每次fsync),测试写文件速度
* ************************************************************/
void write_write4_1();
/****************************************************************
* 功能: 用mmap每次映射4M内存,测试写文件速度
* ************************************************************/
void mmap_write5();
/****************************************************************
* 功能: 用mmap每次一次性映射全部文件,测试写文件速度
* ************************************************************/
void mmap_write5_1();
/****************************************************************
* 功能: 用mmap每次映射1M内存,测试写文件速度
* ************************************************************/
void mmap_write5_2();
/****************************************************************
* 功能: 用mmap每次映射2M内存,测试写文件速度
* ************************************************************/
void mmap_write5_3();
//void cout_write8();
void setRecordData(char *pStrBuf, unsigned long ulBufSize, char *pStrLeftDataBuf, unsigned short &usDataBufSize);
int main(int argc, char *argv[])
&&& int iFlowL
&&& int iNum = 1;
&&& if(2==argc && 0==strcmp(&--help&, argv[1]))
&&&&& printf(&\nusage:&
&&&&&&&&&&&&& &\n\t run all :&&&&&&&&&&&&& ./a.out &
&&&&&&&&& &\n\t run fprintf_write1():& ./a.out 1&&&
&&&&&&&&&&&&&&& &\n\t run fwrite_write2():&& ./a.out 2&&
&&&&&&&&&&&&&&& &\n\t run fwrite_write2_1(): ./a.out 2.1&
&&&&&&&&&&&&&&& &\n\t run write_direct3():&& ./a.out 3&&
&&&&&&&&&&&&&&& &\n\t run write_write4():&&& ./a.out 4&
&&&&&&&&&&&&&&& &\n\t run write_write4_1():& ./a.out 4.1&
&&&&&&&&&&&&&&& &\n\t run mmap_write5():&&&& ./a.out 5&
&&&&&&&&&&&&&&& &\n\t run mmap_write5_1():&& ./a.out 5.1&
&&&&&&&&&&&&&&& &\n\t run mmap_write5_2():&& ./a.out 5.2&
&&&&&&&&&&&&&&& &\n\t run mmap_write5_3():&& ./a.out 5.3\n&
&&&&& exit(0);
&&& if(3==argc)
&&&&& iNum = atoi(argv[2]);
&&& iFlowLen = sprintf(g_strUserFlow,
&&&& &%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s&
&& &\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s&
&& &\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s&
&& &\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s&
&& &\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s&
&& &\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s&
&& &\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s&
&& &\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s&
&& &\t%s\t%s\t%s\t\n&,
&& &.0&, & 16:44:58&, &.0&, &.0&, &059&,
&& &00a5&, &&, &&, &Q d 7&, &59&,
&& &2&, &0&, &1&, & -1&, &G02&,
&& &S38&, &-1&, &-1&, &0&, &-1&,
&& &-1&, &123&, &115.168.25.12&, &172.26.187.5&, &366B0004&,
&& &366B0004413E&, &10.173.8.248&, &10.0.0.200&, &6&, &52042&,
&& &80&, &102&, &3000000&, &14&, &14&,
&& &-&, &-&, &1&, &8&, &7&,
&& &1362&, &5230&, &1362&, &5230&, &0&,
&& &0&, &0&, &0&, &0&, &s.image.&,
&& &0&, &0&, & 0&, & -&,
&& &-&, &-&, & -&, &14&, &14&,
&& &0&, &wap1&, &0&, &-&, & -&,
&& &-&, &PDSN_02&, &延安BSC2&, &0&, &0&,
&&&&&&&&&&& &&, &-1&, &&, &-1&, &-1&,
&& &-1&, &0&, &-1&, &1491300&, &&,
&& &0&, &a8:c000:6380:0:ac1a:ba04:8b00:0/:63:8000:ac:1aba:48b:670&,&-&);
&&& printf(&filesize = %lu\n&, COUNT*(STR_LEN-1));
#ifdef WRITE_FILE
&&& printf(&define WRITE_FILE\n\n&);
&&& printf(&not define WRITE_FILE\n\n&);
& while(iNum--)
&&& if(1==argc)
&&&&& fprintf_write1();&&
&&&&&&& fwrite_write2();
&&&&&&& fwrite_write2_1();
&&&&&&& write_direct3();
&&&&&&& write_write4();
&&&&&&& write_write4_1();
&&&&&&& mmap_write5();
&&&&&&& mmap_write5_1();
&&&&&&& mmap_write5_2();
&&&&&&& mmap_write5_3();
&&&& else if(1 == atof(argv[1]))
&&&&&& fprintf_write1();&&
&&&& else if(2 == atof(argv[1]))
&&&&&& fwrite_write2();&&
&&&& else if(2.1 == atof(argv[1]))
&&&&&& fwrite_write2_1();&&
&&&& else if(3 == atof(argv[1]))
&&&&&& write_direct3();&&
&&&& else if(4 == atof(argv[1]))
&&&&&& write_write4();&&
&&&& else if(4.1 == atof(argv[1]))
&&&&&& write_write4_1();&&
&&&& else if(5 == atof(argv[1]))
&&&&&& mmap_write5();&&
&&&& else if(5.1 == atof(argv[1]))
&&&&&& mmap_write5_1();&&
&&&& else if(5.2 == atof(argv[1]))
&&&&&& mmap_write5_2();&&
&&&& else if(5.3 == atof(argv[1]))
&&&&&& mmap_write5_3();&&
&&& //cout_write8();
double diffTime(struct timeval struStop, struct timeval struStart)
&&& return ( (double)((*struStop.tv_sec + struStop.tv_usec) - (*struStart.tv_sec + struStart.tv_usec)) )/;
void fprintf_write1()
&&& int i = 0;
&&& FILE *fp = NULL;
&&& struct timeval struS
&&& struct timeval struS
&&& char strData[1024+1] = {0};&
&&& gettimeofday(&struStart, NULL);
&&& unsigned long& ulStart = clock();&
&&& fp = fopen(&data.txt&, &w+&);
&&& char strLeftData[2048] = {0};&&
&&& unsigned short strSize = sizeof(strLeftData);
&&& for (i =0; i& COUNT*usPageS i++)
&&&&&&&& setRecordData(strData, 1024+1, strLeftData, strSize);& // 每次写1024 ,'\0' 不写入
#ifdef WRITE_FILE
&&&&&&& fprintf(fp, &%s&, strData);
&&& fclose(fp);
&&& printf(&fprintf_write1:\t %.3lf (clock time)\n&, double(clock()-ulStart)/CLOCKS_PER_SEC);
&&& gettimeofday(&struStop, NULL);
&&& printf(&all time diff:&& %.3lf\n\n&, diffTime(struStop, struStart));
void fwrite_write2()
&&& int i = 0;
&&& FILE *fp = NULL;
&&& unsigned long& ulS
&&& struct timeval struS
&&& struct timeval struS
&&& char strData[1024+1] = {0};&
&&& char strLeftData[2048] = {0};&&
&&& unsigned short strSize = sizeof(strLeftData);
&&& gettimeofday(&struStart, NULL);
&&& ulStart = clock();
&&& fp = fopen(&data.txt&, &w+&);
&&& for (i=0; i&COUNT*usPageS i++)
&&&&&&& setRecordData(strData, 1024+1, strLeftData, strSize);
#ifdef WRITE_FILE
&&&&&&& fwrite(strData, 1024, 1, fp);
&&& fclose(fp);
&&& printf(&fwrite_write2:\t %.3lf (clock time),using system buffer\n&, double(clock()-ulStart)/CLOCKS_PER_SEC);
&&& gettimeofday(&struStop, NULL);
&&& printf(&all time diff:&& %.3lf\n\n&, diffTime(struStop, struStart));
void fwrite_write2_1()&&
&&& //char *pTmp = NULL;
&&& FILE *fp = NULL;
&&& unsigned long& ulS
&&& struct timeval struS
&&& struct timeval struS
&&& char strData[STR_LEN] = {0};&& //4M&
&&& char strLeftData[2048] = {0};&&
&&& unsigned short strSize = sizeof(strLeftData);&&
&&& gettimeofday(&struStart, NULL);
&&& ulStart = clock();
&&&& fp = fopen(&data.txt&, &w+&);
&&& if (NULL == fp)
&&&&&&& perror(&Cannot open file : &);
&&& for (i=0; i&COUNT; i++)
&&&&&&& setRecordData(strData, STR_LEN , strLeftData, strSize);&
#ifdef WRITE_FILE
&&&&&&& fwrite(strData, STR_LEN-1, 1, fp);&& // 每次写入4M
&&&&&&& fflush(fp);
&&& fclose(fp);
&&& printf(&fwrite_write2_1: %.3lf (clock time), allocate 4M, every time fflush()\n&, double(clock()-ulStart)/CLOCKS_PER_SEC);
&&& gettimeofday(&struStop, NULL);
&&& printf(&all time diff:&& %.3lf\n\n&, diffTime(struStop, struStart));
void write_direct3()&&
&&& //char strTmp[1024];
&&& //char *pTmp = NULL;
&&& int fd = -1, ret = -10;
&&& unsigned long& ulS
&&& struct timeval struS
&&& struct timeval struS
&&& //char strData[STR_LEN] = {0};
&&& char *buf = NULL;
&&& char strLeftData[2048] = {0};&&
&&& unsigned short strSize = sizeof(strLeftData);
&&& //buf = (char *)valloc(1024*usPageSize);&&
&&& posix_memalign((void**)&buf, usPageSize, STR_LEN );
&&& //typedef char _aligned_char __attribute__ ((aligned (8192)));
&&& //_aligned_char buf[1024*usPageSize];&&&
&&& gettimeofday(&struStart, NULL);
&&& ulStart = clock();
&&& fd = open(&data.txt&, O_RDWR |O_CREAT|O_DIRECT, 00600);
&&& if (fd&0)
&&&&&&& perror(&Cannot open file : &);
&&& for (i=0; i&COUNT; i++)
&&&&&&& memset(buf, 0, 1024*usPageSize);
&&&&&&& setRecordData(buf, STR_LEN, strLeftData, strSize);&&&&&&&
#ifdef WRITE_FILE
&&&&&&& write(fd, buf, STR_LEN-1);
&&& free(buf);&&&
&&& close(fd);
&&& printf(&write_direct3:\t %.3lf (clock time),using O_DIRECT, allocate 4M\n&, double(clock()-ulStart)/CLOCKS_PER_SEC);
&&& gettimeofday(&struStop, NULL);
&&& printf(&all time diff:&& %.3lf\n\n&, diffTime(struStop, struStart));
void write_write4()
&&& int i = 0;
&&& unsigned long& ulS
&&& struct timeval struS
&&& struct timeval struS
&&& char strData[1024+1] = {0};
&&& char strLeftData[2048] = {0};&&
&&& unsigned short strSize = sizeof(strLeftData);
&&& gettimeofday(&struStart, NULL);
&&& ulStart = clock();
&&& fd = open(&data.txt&, O_CREAT | O_RDWR);
&&& for (i=0; i&COUNT*usPageS i++)
&&&&&&& setRecordData(strData, 1024+1 , strLeftData, strSize);
#ifdef WRITE_FILE
&&&&&&& write(fd, strData, 1024);
&&& close(fd);
&&& printf(&write_write4:\t %.3lf (clock time), using system buffer\n&, double(clock()-ulStart)/CLOCKS_PER_SEC);
&&& gettimeofday(&struStop, NULL);
&&& printf(&all time diff:&& %.3lf\n\n&, diffTime(struStop, struStart));
void write_write4_1()&&
&&& //char buf[1024*usPageSize]; //4M&&&&&&&&
&&& int fd = -1, ret = -10;
&&& unsigned long& ulS
&&& struct timeval struS
&&& struct timeval struS
&&& char strData[STR_LEN] = {0};
&&& char strLeftData[2048] = {0};&&
&&& unsigned short strSize = sizeof(strLeftData);
&&& //memset(buf, 0, 1024*usPageSize);
&&& gettimeofday(&struStart, NULL);
&&& ulStart = clock();
&&& fd = open(&data.txt&, O_RDWR |O_CREAT, 00600);
&&& if (fd&0)
&&&&&&& perror(&Cannot open file : &);
&&& for (i=0; i&COUNT; i++)
&&&&&&& setRecordData(strData, STR_LEN , strLeftData, strSize);
#ifdef WRITE_FILE
&&&&&&& write(fd, strData, STR_LEN-1);&& /*一次写4M*/
&&&&&&& fsync(fd);
&&& close(fd);
&&& printf(&write_write4_1:\t %.3lf (clock time), alloc 4M buffer, every time using fsync()\n&, double(clock()-ulStart)/CLOCKS_PER_SEC);
&&& gettimeofday(&struStop, NULL);
&&& printf(&all time diff:&& %.3lf\n\n&, diffTime(struStop, struStart));
void mmap_write5()
&&& int i = 0, j = 0;
&&& char *pMap = NULL;
&&& int iP
&&& unsigned long& ulS
&&& struct timeval struS
&&& struct timeval struS
&&& char strData[STR_LEN] = {0};
&&& char strLeftData[2048] = {0};&&
&&& unsigned short strSize = sizeof(strLeftData);
&&& //iPagesize = getpagesize();&
&&& gettimeofday(&struStart, NULL);
&&& ulStart = clock();
&&& fd = open(&data.txt&,O_RDWR|O_CREAT,00600);
&&& ftruncate(fd,& COUNT*(STR_LEN-1));
#ifndef WRITE_FILE
&&& pMap = (char *)calloc(1, STR_LEN-1);
&&& for (i = 0; i & COUNT; i++)
#ifdef WRITE_FILE
&&&&&&&& pMap = (char *)mmap(0,STR_LEN-1, PROT_WRITE, MAP_SHARED, fd, i*(STR_LEN-1)); // 每次打开4M
&&&&&&&& if(MAP_FAILED == pMap)
&&&&&&&& {
&&&&&&&&&&&& perror(&mmap_write5 error mmap() :&);
&&&&&&&&&&&&
&&&&&&&& }
&&&&&&&& //ftruncate(fd,& i*(STR_LEN-1) + (STR_LEN-1) );
&&&&&&&& setRecordData(strData, STR_LEN , strLeftData, strSize);
&&&&&&&& memcpy(pMap , strData, STR_LEN-1);
#ifdef WRITE_FILE
&&&&&&&& munmap(pMap, STR_LEN-1);
&&& close(fd);
#ifndef WRITE_FILE
&&& free(pMap);
&&& printf(&mmap_write5:&&&& %.3lf (clock time), mmap: 4M buffer\n&, double(clock()-ulStart)/CLOCKS_PER_SEC);
&&& gettimeofday(&struStop, NULL);
&&& printf(&all time diff:&& %.3lf\n\n&, diffTime(struStop, struStart));
void mmap_write5_1()
&&& int i = 0, j = 0;
&&& char *pMap = NULL;
&&& int iP
&&& unsigned long& ulS
&&& struct timeval struS
&&& struct timeval struS
&&& char strData[STR_LEN] = {0};
&&& char strLeftData[2048] = {0};&&
&&& unsigned short strSize = sizeof(strLeftData);
&&& //iPagesize = getpagesize();&&&
&&& gettimeofday(&struStart, NULL);
&&& ulStart = clock();
#ifndef WRITE_FILE
&&& pMap = (char *)calloc(1, 1024*usPageSize*COUNT);
&&& if(NULL==pMap)
&&&&&&& printf(&pMap calloc error\n\n&);
&&& fd = open(&data.txt&,O_RDWR|O_CREAT,00600);
#ifdef WRITE_FILE
&&& pMap = (char *)mmap(0,(STR_LEN-1)*COUNT, PROT_WRITE, MAP_SHARED, fd, 0); // 一次全部打开文件映射(内存要求)
&&& ftruncate(fd,& (STR_LEN-1)*COUNT );
&&& for (j=0;& j&COUNT; j++)
&&& {&&&&&&&
&&&&&&& setRecordData(strData, STR_LEN , strLeftData, strSize);
&&&&&&& memcpy(pMap + j*(STR_LEN-1), strData, STR_LEN-1);
#ifdef WRITE_FILE
&&& munmap(pMap, (STR_LEN-1)*COUNT);
&&& close(fd);
#ifndef WRITE_FILE
&&& free(pMap);
&&& printf(&mmap_write5_1:\t %.3lf (clock time), mmap: all buffer\n&, double(clock()-ulStart)/CLOCKS_PER_SEC);
&&& gettimeofday(&struStop, NULL);
&&& printf(&all time diff:&& %.3lf\n\n&, diffTime(struStop, struStart));
void mmap_write5_2()
&&& int i = 0;
&&& char *pMap = NULL;
&&& int iP
&&& unsigned long& ulS
&&& struct timeval struS
&&& struct timeval struS
&&& char strData[STR_LEN] = {0};
&&& char strLeftData[2048] = {0};&&
&&& unsigned short strSize = sizeof(strLeftData);
&&& gettimeofday(&struStart, NULL);
&&& ulStart = clock();
#ifndef WRITE_FILE
&&& pMap = (char *)calloc(1, (STR_LEN-1)/4);
&&& fd = open(&data.txt&,O_RDWR|O_CREAT,00600);
&&& ftruncate(fd,& COUNT*(STR_LEN-1));
&&& for (i = 0; i & 4*COUNT; i++)
#ifdef WRITE_FILE
&&&&&&&& pMap = (char *)mmap(0,(STR_LEN-1)/4, PROT_WRITE, MAP_SHARED, fd, i*(STR_LEN-1)/4); // 每次打开1M
&&&&&&&& //ftruncate(fd,& i*(STR_LEN-1)/4 + (STR_LEN-1)/4 );
&&&&&&&& setRecordData(strData, (STR_LEN-1)/4 +1 , strLeftData, strSize);
&&&&&&&& memcpy(pMap , strData, (STR_LEN-1)/4);
#ifdef WRITE_FILE
&&&&&&&& munmap(pMap, (STR_LEN-1)/4);
&&& close(fd);
#ifndef WRITE_FILE
&&& free(pMap);
&&& printf(&mmap_write5_2:&& %.3lf(clock time) (clock time), mmap: 1M buffer\n&, double(clock()-ulStart)/CLOCKS_PER_SEC);
&&& gettimeofday(&struStop, NULL);
&&& printf(&all time diff:&& %.3lf\n\n&, diffTime(struStop, struStart));
void mmap_write5_3()
&&& int i = 0;
&&& char *pMap = NULL;
&&& int iP
&&& unsigned long& ulS
&&& struct timeval struS
&&& struct timeval struS
&&& char strData[STR_LEN] = {0};
&&& char strLeftData[2048] = {0};&&
&&& unsigned short strSize = sizeof(strLeftData);
&&& gettimeofday(&struStart, NULL);
&&& ulStart = clock();
#ifndef WRITE_FILE
&&& pMap = (char *)calloc(1, 512*usPageSize);
&&& fd = open(&data.txt&,O_RDWR|O_CREAT,00600);
&&& ftruncate(fd,& COUNT*(STR_LEN-1));
&&& for (i = 0; i & 2*COUNT; i++)
#ifdef WRITE_FILE
&&&&&&&& pMap = (char *)mmap(0,(STR_LEN-1)/2, PROT_WRITE, MAP_SHARED, fd, i*(STR_LEN-1)/2); // 每次打开2M
&&&&&&&& //ftruncate(fd,& i*(STR_LEN-1)/2 + (STR_LEN-1)/2 );
&&&&&&&& setRecordData(strData, (STR_LEN-1)/2 +1 , strLeftData, strSize);
&&&&&&&& memcpy(pMap , strData, (STR_LEN-1)/2);
#ifdef WRITE_FILE
&&&&&&&& munmap(pMap, (STR_LEN-1)/2);
&&& close(fd);
#ifndef WRITE_FILE
&&& free(pMap);
&&& printf(&mmap_write5_3:&&&& %.3lf (clock time), mmap: 2M buffer\n&, double(clock()-ulStart)/CLOCKS_PER_SEC);
&&& gettimeofday(&struStop, NULL);
&&& printf(&all time diff:&& %.3lf\n\n&, diffTime(struStop, struStart));
void setRecordData(char *pStrBuf, unsigned long ulBufSize, char *pStrLeftDataBuf, unsigned short &usDataBufSize)
&&& unsigned long ulLeftBufLen = ulBufS
&&& short sCopyLen = 0;
&&& unsigned short usLeftCharLen = 0;
&&& char *pStrBufTmp = pStrB
&&& char *pStrTmp = pStrLeftDataB
&&& if (NULL==pStrBuf || NULL==pStrLeftDataBuf)
&&& sCopyLen = snprintf(pStrBufTmp, ulLeftBufLen, pStrTmp);
&&& if(sCopyLen&0)
&&&&&&& perror(&snprintf error: &);
&&& if( sCopyLen & ulLeftBufLen-1 )
&&&&&&& ulLeftBufLen = ulLeftBufLen - sCopyL
&&&&&&& pStrBufTmp += sCopyL
&&&&&&& pStrTmp = g_strUserF
&&&&&&& while(ulLeftBufLen & 0)
&&&&&&&&&&& sCopyLen = snprintf(pStrBufTmp, ulLeftBufLen, pStrTmp);
&&&&&&&&&&& if(sCopyLen&0)
&&&&&&&&&&& {
&&&&&&&&&&&&&&& perror(&snprintf error: &);
&&&&&&&&&&&&&&&
&&&&&&&&&&& }
&&&&&&&&&&& if( sCopyLen & ulLeftBufLen-1 )
&&&&&&&&&&& {
&&&&&&&&&&&&&&& ulLeftBufLen = ulLeftBufLen - sCopyL
&&&&&&&&&&&&&&& pStrBufTmp += sCopyL
&&&&&&&&&&& }
&&&&&&&&&&& else
&&&&&&&&&&& {
&&&&&&&&&&&&&&&
&&&&&&&&&&& }
&&& /*剩下的拷到pStrLeftDataBuf*/
&&& strncpy(pStrLeftDataBuf, pStrTmp + (ulLeftBufLen-1), usDataBufSize-1 );& /*确保:pStrLeftDataBuf[usDataBufSize-1] = '\0'*/
写文件速度测试:
函数写文件方法简介:
fprintf_write1()
&&&&&&&& 用fprintf函数写文件,测试写文件速度。
fwrite_write2()
&&&&&&&& 用fwrite函数写文件,使用系统缓存,测试写文件速度。
fwrite_write2_1(),
&&&&&&&& 用fwrite函数写文件,自己分配4M缓存,每次写入4M,然后调用fflush,测试写文件速度。
write_direct3();
&&&&&&&& 用write写文件,设0_DIRECT,自己维护4M的缓存,测试写文件速度。
write_write4();
&&&&&&&& 用write写文件,使用系统缓存,测试写文件速度。,
write_write4_1();
&&&&&&&& 用write写文件,自己分配4M缓存,每次写入4M,然后调用fsync,测试写文件速度。,
mmap_write5();
&&&&&&&& 用mmap每次映射4M内存,测试写文件速度。
mmap_write5_1();
用mmap每次映射全部文件大小的内存,测试写文件速度。
mmap_write5_2();
&&&&&&&& 用mmap每次映射1M内存,测试写文件速度。,
mmap_write5_3();
用mmap每次映射2M内存,测试写文件速度。
测试命令:
/usr/bin/time -f &CPU: %P& ./a.out 1
测试结果:
,写入文件大小:
fprintf_write1()
) = 16.255
fwrite_write2()
) = 18.676
fwrite_write2_1(),
) = 16.714
write_direct3();
) = 19.290
write_write4();
write_write4_1();
)& = 24.918
mmap_write5()
)& = 16.794
mmap_write5_1()
)& = 55.473
mmap_write5_2()
mmap_write5_3()
mmap_write5_2()最快
,写入文件大小:
实际执行时间
fprintf_write1()
fwrite_write2()
fwrite_write2_1()
write_direct3()
write_write4()
write_write4_1()
mmap_write5()
mmap_write5_1()
mmap_write5_2()
mmap_write5_3()
mmap_write5_2()最快
总结: 用mmap每次映射1M内存,测试得到最快速度。
已发表评论数()
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
标题不准确
排版有问题
主题不准确
没有分页内容
图片无法显示
视频无法显示
与原文不一致}

我要回帖

更多关于 fwrite连续写入 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信