C_and_CPP 板


LINE

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
1F:→ jaw109:VS2008有所謂的"純C"嗎, 那std::printf("hello")算不算純C 04/11 16:33
2F:→ chrisdar:盡量不要用windows.h API thanks 04/11 16:33
問題可能沒敘述好 抱歉 重新整理一次 追加一個問題 是不是增加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)
3F:→ ewn:google的結果,似乎大家都認為改stl的buf沒啥用處... 04/11 19:43
4F:→ ewn:自己弄個buffer可能好一點,剛試了一下寫的部分 04/11 19:44
5F:→ ewn:就算用string當緩衝,都能快個3倍,自己寫buffer應該更快吧 04/11 19:44
6F:→ ewn:話說回來,我直接用你的code丟進vc2008sp1,讀寫都一樣慢@.@ 04/11 19:46
7F:→ chrisdar:似乎 速度問題出在 數字<->字串 之間的互轉上面 04/11 20:10
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)







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:Gossiping站內搜尋

TOP