看板 C_and_CPP 關於我們 聯絡資訊
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) Linux Scientific 6.2 , GCC 4.4.6 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): 由於研究的關係我寫了一個測試fwrite的小程式,每次寫入51200Byte大小的binary資料 ,程式碼如下,結果顯示大部分每次花0.1ms,但有時候會有100~200ms的出現,不知道 這種現象造成的原因是什麼? 如果需要長時間穩定寫入時間,是否有其他方式可以處理? 程式碼(Code):(請善用置底文網頁, 記得排版) #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; } -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 180.177.9.172 ※ 文章網址: http://www.ptt.cc/bbs/C_and_CPP/M.1402507086.A.64E.html
diabloevagto:我猜是實際寫到硬碟才花較久 06/12 01:35
TeaEEE:時間的差異應該是硬碟的暫存造成的 06/12 01:36
h520:組NAS/RAID 使用logical volume理論上比較穩 但是要慎選廠牌 06/12 06:51
bobhsiao:fwrite會先進buffer, 不妨試 open/write 06/12 17:17
damody:正常的 06/12 18:46
damody:換ssd吧 06/12 18:47