编写许多txt文件(90),每个文件5MB,最多需要1400秒 [英] Writing a lot of txt files (90), 5MB per file, takes up to 1400s
问题描述
我是电气工程学生。
作为一个配置,我需要实现医学成像中使用的背投影算法来形成图像。
要计算最终图像,将计算一个很多信息并保存在一个向量中。
在某个所需的分辨率(256 * 256像素)的最终图像,程序崩溃,因为我用完了RAM,所以我决定将此信息写入90个文本文件。
As an assigment I need to implement the back projection algorithm used in medical imaging to form in image. To calculate the final image a lot information is calculated and kept in a vector. At a certain desired resolution (256*256 pixels) of the final image, the program crashes as I run out of RAM, so I decided to write this information to 90 text files.
我使用stream来编写这些文件。
I use ofstream to write these file.
计算此信息,然后将其存储在向量中所需的时间是:
The time needed to calculate this information and then storing it in the vector is:
- 输出分辨率64x64:13,4s
- 输出分辨率128x128:140s
将此信息写入.txt文件中:
Writing this information in .txt files:
- 64x64 156s MB /档案)
- 128x128 1400s(5MB /档)
ofstream file;
for(k = 0; k < 90; k++)
{
oss.str(""); //string stream
oss << "rec\\reconstruction_matrix_step"<< k << ".txt" ; // per step other file
filename = path;
filename.append(oss.str());
file.open(filename.c_str());
double weight;
for( l = 0; l < resolution; l ++)
{
bestand << "Begin " << l << endl;
l_border = - WIDTH*(resolution*1.0/2.0 - l);
r_border = - WIDTH*(resolution*1.0/2.0 - l) + WIDTH;
for(i = 0; i < resolution; i++)
{
for(j = 0; j < resolution; j++)
{
file << getSurface(pixels[i][j], l_border, r_border) << "\t";
}
file << "\n";
}
file << "End" << l << "\n\n\n";
}
file.close();
}
<
When I use a vector, getSurface(pixels[i][j], l_border, r_border) is put in a vector instead of being written to a file.
有什么方法可以加快这个进程吗?
Is there any way I can speed up this proces?
推荐答案
尝试将文本格式更改为二进制格式;这可能会大大减少文件大小(和文件写入时间)。
Try changing the format from text to binary; this might reduce file size (and file writing time) greatly.
file.open(filename.c_str(), ios_base::binary);
...
// The following writes a vector into a file in binary format
vector<double> v;
const char* pointer = reinterpret_cast<const char*>(&v[0]);
size_t bytes = v.size() * sizeof(v[0]);
file.write(pointer, bytes);
这篇关于编写许多txt文件(90),每个文件5MB,最多需要1400秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!