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灯, 水草

请输入看板名称,例如:Soft_Job站内搜寻

TOP