低水準入出力の速度比較
K&R本 演習8-1
とりあえずファイルのオープンだけでやってみました。
#include <io.h> #include <fcntl.h> #include <stdio.h> #include "mylib.h" #define BUFSIZE 1024 void stdio_cp (void) { FILE *fp; errno_t err; char buf[BUFSIZE]; if ( (err = fopen_s(&fp,"test1.txt","r")) != 0 ) { puts("file open error"); return; } while ( fgets(buf,sizeof(buf),fp) != NULL ) { //printf("%s",buf); } fclose(fp); } void io_cp (void) { int fh1; int n; char buf[BUFSIZE]; if ( (fh1 = _open("test1.txt",_O_RDONLY)) == -1 ) { puts("file open error"); return; } while( (n = _read(fh1,buf,BUFSIZE)) > 0 ) { //printf("%s",buf); } _close(fh1); } int main (void) { int count = 1000; printf("stdio: %f\n",benchmark(count,stdio_cp)); printf("io : %f\n",benchmark(count,io_cp)); return 0; }
$ main stdio: 2.671000 io : 1.735000
少し速い程度ですね。もっと速いのかと思いました。
であれば普通にfopen_s等を使うべきですね。