我有一段代码可以分析来自非常大(10-100GB)二进制文件的数据流.它运行良好,所以是时候开始优化了,目前磁盘IO是最大的瓶颈.
有两种类型的文件正在使用中.第一种类型的文件由16位整数流组成,必须在I/O之后进行缩放,以转换为物理上有意义的浮点值.我以块的形式读取文件,并通过一次读取一个16位代码,执行所需的缩放,然后将结果存储在数组中来读取数据块.代码如下:
int64_t read_current_chimera(FILE *input, double *current,
int64_t position, int64_t length, chimera *daqsetup)
{
int64_t test;
uint16_t iv;
int64_t i;
int64_t read = 0;
if (fseeko64(input, (off64_t)position * sizeof(uint16_t), SEEK_SET))
{
return 0;
}
for (i = 0; i < length; i++)
{
test = fread(&iv, sizeof(uint16_t), 1, input);
if (test == 1)
{
read++;
current[i] = chimera_gain(iv, daqsetup);
}
else
{
perror("End of file reached");
break;
}
}
return read;
}
Run Code Online (Sandbox Code Playgroud)
chimera_gain函数只需要一个16位整数,对其进行缩放并返回双精度进行存储.
第二种文件类型包含64位双精度数,但它包含两列,其中我只需要第一列.要做到这一点,我会双击双打并丢弃第二个双打.双重必须在使用前进行字节交换.我用来做这个的代码如下:
int64_t read_current_double(FILE *input, double *current, int64_t position, int64_t length)
{
int64_t test;
double iv[2];
int64_t i;
int64_t read = 0;
if (fseeko64(input, (off64_t)position * 2 * sizeof(double), SEEK_SET))
{
return 0;
}
for (i = 0; i < length; i++)
{
test = fread(iv, sizeof(double), 2, input);
if (test == 2)
{
read++;
swapByteOrder((int64_t *)&iv[0]);
current[i] = iv[0];
}
else
{
perror("End of file reached: ");
break;
}
}
return read;
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以建议一种阅读这些文件类型的方法,这种方法比我目前正在做的快得多吗?
首先,使用配置文件r 来识别程序中的热点会很有用。根据您对问题的描述,您因 fread 的数量而产生了大量的开销。由于文件很大,增加每个 io 读取的数据量会有很大的好处。
通过将两个读取流的小程序放在一起来说服自己这一点。
1) read it as you are in the example above, of 2 doubles.
2) read it the same way, but make it 10,000 doubles.
Run Code Online (Sandbox Code Playgroud)
时间都运行了几次,并且您可能会观察到#2 运行得更快。
祝你好运。