2017-03-08 55 views
0

最近我想寫點雲數據在Visual Studio 2010中使用C++在我使用的ostream輸出數據開始到.txt文件,但我發現它慢當寫入數據時。如何快速寫入大量數據轉化爲txt文件

我的代碼:

std::ofstream outfile; 
outfile.open(filename.c_str()); 
for(int index = 0;index < pointcloud.size();index++){ 
    outfile<<pointcloud[index].x<<pointcloud[index].y<<pointcloud[index].z 
    <<pointcloud[index].r<<pointcloud[index].g<<pointcloud[index].b<<'\n'; 
} 
outfile<<std::endl; 

輸出點雲是非常巨大的,幾乎是0.5G。寫入.txt文件需要幾分鐘的時間。如何提高寫入數據的速度?我認爲這可能是緩存緩衝區大小的問題,但不確定。有人可以幫我弄這個嗎?

+0

你可以使用std ::副本以提高性能。 –

+0

是對源數據('pointcloud')所有的內存中,或者是它的容器包裝以「基於雲」的數據?如果是後者,你的問題可能在那裏? – franji1

+3

獲得更快的硬盤和更多的RAM。您的性能受到硬盤驅動器速度的限制。改變這個宇宙的物理定律之後,唯一的答案就是更快的硬件。 –

回答

0
I have found a way to solve this problem myself.The performance bottlenecks is 

不是IO.If引起我用OUTFILE < <(的std :: string)str.c_str(); (str是一個長字符串 像200MB),這可能需要小於一second.so我使用多線程到 數據拼接成一個長字符串和輸出通過IO stream.The速度上的4core增加 約6倍電腦。