user-0 提问: fwrite效率太低（一次写50M需要6秒），有啥好方法么？

回复:
user: user-1
content: setvbuf Controls stream buffering and buffer size.  int setvbuf( FILE *stream, char *buffer, int mode, size_t size );  Routine Required Header Compatibility  setvbuf <stdio.h> ANSI, Win 95, Win NT    For additional compatibility information, see Compatibility in the Introduction.  Libraries  LIBC.LIB Single thread static library, retail version  LIBCMT.LIB Multithread static library, retail version  MSVCRT.LIB Import library for MSVCRT.DLL, retail version    Return Value  setvbuf returns 0 if successful, or a nonzero value if an illegal type or buffer size is specified.  Parameters  stream  Pointer to FILE structure  buffer  User-allocated buffer  mode  Mode of buffering  size  Buffer size in bytes. Allowable range: 2 < size < 32768. Internally, the value supplied for size is rounded down to the nearest multiple of 2.  Remarks  The setvbuf function allows the program to control both buffering and buffer size for stream. stream must refer to an open file that has not undergone an I/O operation since it was opened. The array pointed to by buffer is used as the buffer, unless it is NULL, in which case setvbuf uses an automatically allocated buffer of length size/2 * 2 bytes.  The mode must be _IOFBF, _IOLBF, or _IONBF. If mode is _IOFBF or _IOLBF, then size is used as the size of the buffer. If mode is _IONBF, the stream is unbuffered and size and buffer are ignored. Values for mode and their meanings are:  _IOFBF  Full buffering; that is, buffer is used as the buffer and size is used as the size of the buffer. If buffer is NULL, an automatically allocated buffer size bytes long is used.  _IOLBF  With MS-DOS, the same as _IOFBF.  _IONBF  No buffer is used, regardless of buffer or size.  Example  /* SETVBUF.C: This program opens two streams: stream1  * and stream2. It then uses setvbuf to give stream1 a  * user-defined buffer of 1024 bytes and stream2 no buffer.  */  #include <stdio.h>  void main( void ) {    char buf[1024];    FILE *stream1, *stream2;     if( ((stream1 = fopen( "data1", "a" )) != NULL) &&        ((stream2 = fopen( "data2", "w" )) != NULL) )    {       if( setvbuf( stream1, buf, _IOFBF, sizeof( buf ) ) != 0 )          printf( "Incorrect type or size of buffer for stream1\n" );       else          printf( "'stream1' now has a buffer of 1024 bytes\n" );       if( setvbuf( stream2, NULL, _IONBF, 0 ) != 0 )          printf( "Incorrect type or size of buffer for stream2\n" );       else          printf( "'stream2' now has no buffer\n" );       _fcloseall();    } }   Output  'stream1' now has a buffer of 1024 bytes 'stream2' now has no buffer   Stream I/O Routines  See Also   fclose, fflush, fopen, setbuf

user: user-2
content: 直接write就可以了。

user: user-3
content: fwrite是很底层的调用，速度应该很快，lz写那么大的数据，应该设置应该比较大的文件缓存区(setvbuf )

user: user-4
content: 不能用1，50K啊，亲，一次写一个1字节，你试试一次10K，速度会快很多的

user: user-1
content: 关闭杀软实时防护！

user: user-5
content: 调整一下缓冲区的大小试试！

user: user-6
content: 实在不行用优化的指令啊，GNU编译器不是有个什么优化级别的命令来着，就是优化后的代码面目全非

user: user-7
content: 这个应该选择一个合适的缓冲区大小吧

user: user-8
content: 那调WriteFile吧，另外调调buffer size。

user: user-9
content: 引用 18 楼 wangch 的回复:请问如何调？用哪个函数？谢谢  [quote=引用 10 楼 mujiok2003 的回复:] 估计流的缓冲区太小了。  假设缓冲区大小为4K, 50M的需要50*1024K/4K = 12500次文件写入。  试试调大缓冲区。[/quote]  http://www.cplusplus.com/reference/cstdio/setvbuf/

user: user-0
content: 目测不到2秒 引用 14 楼 ForestDB 的回复:LZ的机器copy一个50M的文件需要多久？

user: user-10
content: 聽過SSD嗎,有錢就是快

user: user-0
content: 请问如何调？用哪个函数？谢谢  引用 10 楼 mujiok2003 的回复:估计流的缓冲区太小了。  假设缓冲区大小为4K, 50M的需要50*1024K/4K = 12500次文件写入。  试试调大缓冲区。

user: user-11
content: 没有这么水吧？你确定不是代码bug？

user: user-0
content: 用的是服务器，硬盘不知道怎么查。 OS 名称:          Microsoft(R) Windows(R) Server 2003, Enterprise Edition 系统类型:         X86-based PC 处理器:           安装了 4 个处理器。                   [01]: x86 Family 6 Model 15 Stepping 11 GenuineIntel ~2128 Mhz                   [02]: x86 Family 6 Model 15 Stepping 11 GenuineIntel ~2128 Mhz                   [03]: x86 Family 6 Model 15 Stepping 11 GenuineIntel ~2128 Mhz                   [04]: x86 Family 6 Model 15 Stepping 11 GenuineIntel ~2128 Mhz  哪位能帮我试一下在自己的电脑上需要多久？谢谢

user: user-12
content: 换一个电脑吧

user: user-8
content: LZ的机器copy一个50M的文件需要多久？

user: user-13
content: 换一块好点的硬盘

user: user-14
content: 引用 1 楼 adlay 的回复:换一块好点的硬盘 引用 2 楼 ggglivw 的回复:换一个电脑吧+1

user: user-15
content: 换机器吧....  $ cat 1.c ; gcc -O2 1.c ; time ./a  ; ll test #include <stdio.h> #include <stdlib.h>  #define BUFFSIZE     51699500  int main() {         FILE *fp   = fopen("test", "wb");         char* buff = (char*)malloc(BUFFSIZE);         int i;          for(i = 0; i < BUFFSIZE; ++i)                 buff[i] = (char)i;          for(i = 0; i < 40; ++i)                 fwrite(buff, 1, BUFFSIZE, fp);          fclose(fp); return 0; }  real    0m1.620s user    0m0.046s sys     0m1.388s -rw-r--r--+ 1 xxxxx None 2.0G Nov 26 17:16 test