[問題] 我的程式中的 ifstream 還能提高速度嗎?
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
04/11 16:33, 1F
→
04/11 16:33, , 2F
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
04/11 19:43, 3F
→
04/11 19:44, , 4F
04/11 19:44, 4F
→
04/11 19:44, , 5F
04/11 19:44, 5F
→
04/11 19:46, , 6F
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)
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章