[問題] 我的程式中的 ifstream 還能提高速度嗎?

看板C_and_CPP (C/C++)作者 (克里斯)時間16年前 (2009/04/11 15:57), 編輯推噓0(007)
留言7則, 3人參與, 最新討論串1/1
http://nopaste.info/0d52394881.html code備份 http://rafb.net/p/GBll8s47.html 問題在文末 謝謝各位不吝指教 #include <iostream> #include <fstream> #include <sstream> #include <ctime> #include <limits> using namespace std; template <typename Elem, typename Tr> inline basic_istream<Elem, Tr>& ignoreline(basic_istream<Elem, Tr>& strm) { return strm.ignore(numeric_limits<streamsize>::max(), strm.widen('\n')); } template <typename Elem, typename Tr> inline basic_istream<Elem, Tr>& ignorespace(basic_istream<Elem, Tr>& strm) { return strm.ignore(numeric_limits<streamsize>::max(), strm.widen(' ')); } template <typename Elem, typename Tr> inline basic_ostream<Elem, Tr>& endx(basic_ostream<Elem, Tr>& strm) { return strm.put(strm.widen('\n')); } int main(int argc, char* argv[]) { srand((unsigned int)time(0)); clock_t myTimer = clock(); ofstream ofs("data.txt"); char *wbuf = new char[0x100000]; ofs.rdbuf()->pubsetbuf(wbuf, 0x100000); myTimer = clock(); for (int i = 0; i < 0x100000; ++i) { ofs << "POINT POINT_" << i << " " << rand() << " " << rand() << " ;\n"; } myTimer = clock() - myTimer; cout << "write data " << myTimer << "ms" << endx; ofs.close(); delete[] wbuf; ifstream ifs("data.txt"); char *rbuf = new char[0x100000]; ifs.rdbuf()->pubsetbuf(rbuf, 0x100000); string p1name; myTimer = clock(); for (int i = 0, x = 0, y = 0; i < 0x100000; ++i) { ifs >> ignorespace >> p1name >> x >> y >> ignoreline; if (0x100000 == i + 1) { cout << p1name << " " << x << "," << y << endx; } } myTimer = clock() - myTimer; cout << "read data " << myTimer << "ms" << endx; ifs.close(); delete[] rbuf; system("pause"); return 0; } /* 環境:Visual Studio 2008 Sp1 + AMD Athlon 64 3000+ Q1:讀寫次數約(讀43寫6839),為什麼寫入緩衝沒有發揮作用? (讀取緩衝卻發生作用?) Q2:效能瓶頸都卡在 >> 這個運算子身上(因為加大緩衝時間卻沒縮短), 該如何改善?(除了用純C >_<) */ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 123.195.88.23

04/11 16:33, , 1F
VS2008有所謂的"純C"嗎, 那std::printf("hello")算不算純C
04/11 16:33, 1F

04/11 16:33, , 2F
盡量不要用windows.h API thanks
04/11 16:33, 2F
問題可能沒敘述好 抱歉 重新整理一次 追加一個問題 是不是增加stream的緩衝大小對於資料讀取速度沒有幫助? 還是緩衝太小效果看不出來? 補充數據 讀取緩衝=32MB 讀取次數=9 時間一樣 ifs >> ignorespace >> p1name >> x >> y >> ignoreline; 以上這行需要怎麼改 速度會改善 ※ 編輯: chrisdar 來自: 123.195.88.23 (04/11 17:02) ※ 編輯: chrisdar 來自: 123.195.88.23 (04/11 17:05)

04/11 19:43, , 3F
google的結果,似乎大家都認為改stl的buf沒啥用處...
04/11 19:43, 3F

04/11 19:44, , 4F
自己弄個buffer可能好一點,剛試了一下寫的部分
04/11 19:44, 4F

04/11 19:44, , 5F
就算用string當緩衝,都能快個3倍,自己寫buffer應該更快吧
04/11 19:44, 5F

04/11 19:46, , 6F
話說回來,我直接用你的code丟進vc2008sp1,讀寫都一樣慢@.@
04/11 19:46, 6F

04/11 20:10, , 7F
似乎 速度問題出在 數字<->字串 之間的互轉上面
04/11 20:10, 7F
clock_t myTimer = clock(); ofstream ofs1("data1.txt"); myTimer = clock(); for (int i = 0; i < 0x100000; ++i) { ofs1 << "0" ; } myTimer = clock() - myTimer; cout << "write data1 " << myTimer << "ms" << endx; ofs1.close(); ofstream ofs2("data2.txt"); myTimer = clock(); for (int i = 0; i < 0x100000; ++i) { ofs2 << 0 ; } myTimer = clock() - myTimer; cout << "write data2 " << myTimer << "ms" << endx; ofs2.close(); /* write data1 625ms write data2 2547ms */ 看來我抓到速度上的問題了 本來數字<->字串之間互轉就慢了 跟 stream 一點關係都沒有 謝謝各位 ※ 編輯: chrisdar 來自: 123.195.88.23 (04/11 20:17)
文章代碼(AID): #19u4rlZz (C_and_CPP)
文章代碼(AID): #19u4rlZz (C_and_CPP)