我对使用C或C ++编写高斯CUBE文件的快速例程感兴趣。目前我所拥有的代码最快的是:
FILE* outfile;
outfile = fopen(recSpace[iPlane].filename.c_str(), "w");
fprintf(outfile, "%s\n", " GAUSSIAN CUBE FORMAT ");
fprintf(outfile, "%s\n", " OUTER LOOP : X, MIDDLE LOOP : Y, INNER LOOP : Z");
fprintf(outfile, "%d %12.6f %12.6f %12.6f\n", 0, x0, y0, z0);
fprintf(outfile, "%5d %10.3f %10.3f %10.3f\n", nx, x1, 0.0, 0.0);
fprintf(outfile, "%5d %10.3f %10.3f %10.3f\n", ny, 0.0, y1, 0.0);
fprintf(outfile, "%5d %10.3f %10.3f %10.3f\n", nz, 0.0, 0.0, z1);
int numbers_per_line = 0;
double* numbers = (double*)malloc(6 * sizeof(double));
for(unsigned int k=0; k<nz; k++)
{
std::cout << "Writing layer " << k + 1 << " out of " << nz << std::endl;
for(unsigned int j=0; j<ny;j++)
{
numbers_per_line = 0;
for(unsigned int i=0; i<nx; i++)
{
int address = i * nx * ny + j * ny + k;
if(numbers_per_line < 6)
{
numbers[numbers_per_line] = layer[address];
numbers_per_line++;
}
if(numbers_per_line == 6)
{
fprintf(outfile, "%g %g %g %g %g %g \n", numbers[0], numbers[1], numbers[2], numbers[3], numbers[4], numbers[5]);
numbers_per_line = 0;
}
}
if(numbers_per_line % 6 != 0)
{
for(int l=0; l<numbers_per_line; l++)
{
fprintf(outfile, "%g", numbers[l]);
}
fprintf(outfile, "\n");
}
}
}
fclose(outfile);
在Windows 7 64位下编写601 * 601 * 601卷大约需要5分钟。有更快的方法吗?
非常感谢!
答案 0 :(得分:0)
如果fprintf()
对您来说很慢,那么映射文件并直接使用内存可能会更有用。但是您必须自己编写float-&gt;字符串转换代码。