如何快速将大量数据写入TXT文件

How to quickly write large amounts of data into txt files

本文关键字:TXT 文件 数据 何快速      更新时间:2023-10-16

最近,我试图通过在Visual Studio 2010中使用C 将PointCloud数据写入.txt文件。在开始时,我使用Ostream来输出数据,但是我发现在编写书写时发现它很慢。数据。

我的代码:

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.5克。写入.txt文件需要几分钟。如何提高写入数据的速度?我认为这可能是缓存缓冲区大小的问题,但不确定。有人可以帮我弄这个吗?

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

不是由io引起的。(str是一个长字符串像200MB一样,这可能需要不到一秒钟。因此,我使用多线程来拼接数据成长为长字符串并通过IO流量输出。速度提高了在4台计算机上大约6次。