作者chrisdar (克里斯)
看板C_and_CPP
标题[问题] 我的程式中的 ifstream 还能提高速度吗?
时间Sat Apr 11 15:57:34 2009
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)