说是过几天到水利研究所参加连调,可惜这几天等着一点信号也没有,有点慢下来了.搞搞自己的...
实验室的arm开发箱虽然样子很大,但是一个开发箱居然都不给原理图.这让人怎么搞啊,想编译个内核,发现连液晶是哪个公司的型号都不知道...还是一步一步来吧...有C8051F做arm的过渡我感觉还不错哈...
晚上回来早点.继续上次的学习.fwrite和write,一个是标准I/O,一个是底层调用.谁快呢?我起初觉得即然用底层调用应该是很快的.不过实践是检验真理的好办法.
下面给出实际测试情况:
先上fwrite:
===========================
代码:
--------------------------------------------------
#include <stdio.h> #define N (1024 * 1024 * 5) int main(void) { char block[N] = {0}; FILE *ofp; int cnt; ofp = fopen("1G_file.out", "w"); for (cnt = 0; cnt < 200; cnt++) fwrite(block, N, 1, ofp); fclose(ofp); printf("1G file creat complete!\n"); return 0; }
-------------------------------------------------------
有多快呢?
测试结果:
--------------------------------------------------------
1G file creat complete!
0.00user 3.65system 0:36.67elapsed 9%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+2048016outputs (0major+1418minor)pagefaults 0swaps
casio$ ls -lh
总用量 1001M
-rw-r--r-- 1 casio casio 1000M 2010-05-15 22:35 1G_file.out
-rwxr-xr-x 1 casio casio 8.9K 2010-05-15 22:34 1G_fpr
-rw-r--r-- 1 casio casio 269 2010-05-15 22:25 1G_fpr.c
===============================================
下面是write:
代码,和上次经过了点修改:
------------------------------------------------------------------
#include <unistd.h> #include <sys/stat.h> #include <fcntl.h> #include <stdlib.h> #include <stdio.h> #define N (1024 * 1024 * 5) int main(void) { char block[N] = {0}; int out,cnt; out = open("1G_file.out", O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR); for (cnt = 0; cnt < 200; cnt++) write(out, block, N); printf("1G file creat complete!\n"); exit(0); }
------------------------------------------------
测试结果如下:
--------------------------------------------------
1G file creat complete!
0.00user 3.42system 0:37.51elapsed 9%CPU (0avgtext+0avgdata 0maxresident)k
8inputs+2048000outputs (0major+1401minor)pagefaults 0swaps
casio:$ ls -lh
总用量 1001M
-rw------- 1 casio casio 1000M 2010-05-15 22:40 1G_file.out
-rwxr-xr-x 1 casio casio 7.7K 2010-05-15 22:39 1G_write
-rw-r--r-- 1 casio casio 359 2010-05-15 22:27 1G_write.c
=======================================
为此我经过了好几次测试,有时候write点,有时候fwrite快点,不过底层的时候稍微多那么几次.但是最大的差距不会超过1秒.所以底层调用和标准I/O看,性能都差不多.
stdio库在FILE结构里使用了一个内部缓冲区,只有在缓冲区满时才进行底层系统调用.