評價: 0 回應: 0 閱覽: 77
置頂

fwrite 寫入時間異常

  由於研究的關係我寫了一個測試fwrite的小程式,每次寫入51200Byte大小的binary資料

  ,程式碼如下,結果顯示大部分每次花0.1ms,但有時候會有100~200ms的出現,不知道

  這種現象造成的原因是什麼? 如果需要長時間穩定寫入時間,是否有其他方式可以處理?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>


int main(){
        FILE *time;
        FILE *test;
        float time_use=0;
        struct timeval start;
        struct timeval end;
        int size;

        int i=0;
        test=fopen("file.dat","wb");
        time=fopen("time1.txt","w");

        for(i=1;i<40000;i++){
                size=51200;
                unsigned char a[size];
                gettimeofday(&start,NULL);
                fwrite(a,sizeof(char),size,test);

                gettimeofday(&end,NULL);
        time_use=(end.tv_sec-start.tv_sec)*1000000+(end.tv_usec-start.tv_usec);
                fprintf(time,"size=%d\ttime=%f\n",size,time_use);

        }

        fclose(time);
        fclose(test);
        return 0;
}

 

會員登入 (先登入會員才能回覆留言喔!)

Facebook留言