看板 C_and_CPP 關於我們 聯絡資訊
※ 引述《mself (mself)》之銘言: : 開發平台(Platform): (Ex: VC++, Gcc, Linux, ...) : Linux + GCC : 問題(Question): : 我寫一個程式,主體是一個迴圈並且從檔案中讀取資料來處理。例如: : while(fin.good()){ : fin >> time; : fin >> voltage; : subfunction(time, voltage); : } : 餵入的資料(Input): : 0000 1.5 : 0002 1.3 : 0003 0.7 : 0009 1.1 : ... : 我有聽說最好把 file I/O 集中,一次讀一批會比較快 : 不曉得大概是怎麼做? : 謝謝~ 給你的 fin 大一點的 buffer;用 fin.rdbuf()->pubsetbuf(buffer, buffer_size) 去設定(順便提一下 non buffering 可以用 fin<<std::unitbuf; 去設定) 基本上用 FILE* 或 std::fstream 的話,系統都會有預設使用 buffer 所以當你呼叫 fread(...) 或 fin.read() 其實都是先從buffer 裡面撈 當 buffer 空了才會動一次底層的 read。 兩者的差異在於 std::Xstream 系列都幫你重載了常用的格式化運算,所以才能 string svar; int ivar; ifstream fin("file"); fin>>ivar; fin>>svar; 說他慢,其實也沒有慢到哪邊去,buffer 夠大,差異就越小。 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.123.101.180
tomap41017:buffer的解好酷!!一般真的很容易忽略它 01/04 22:08