【发布时间】:2023-03-26 03:15:02
【问题描述】:
我发现使用带有 'int24' 选项的 Matlab 'fread' 读取以 24 位整数格式打包的数据需要很多时间。我发现,如果我在“int32”或“int16”或“int8”中读取数据,与“int24”相比,读取时间非常快。有没有更好的方法来减少读取 24 位整数数据的时间?
为了了解问题,下面给出了示例代码。
clear all; close all; clc;
% generate some data and write it as a binary file
n=10000000;
x=randn(n,1);
fp=fopen('file1.bin', 'w');
fwrite(fp, x);
fclose(fp);
% read data in 24-bit format and measure the time
% please note that the data we get here will be different from 'x'.
% The sole purpose of the code is to demonstrate the delay in reading
% 'int24'
tic;
fp=fopen('file1.bin', 'r');
y1=fread(fp, n, 'int24');
fclose(fp);
toc;
% read data in 32-bit format and measure the time
% please note that the data we get here will be different from 'x'.
% The sole purpose of the code is to demonstrate the delay in reading
% 'int24'
tic;
fp=fopen('file1.bin', 'r');
y2=fread(fp, n, 'int32');
fclose(fp);
toc;
输出内容如下: 经过的时间是 1.066489 秒。 经过的时间是 0.047944 秒。
虽然 'int32' 版本读取更多数据(32*n 位),但它比 'int24' 读取速度快 25 倍。
【问题讨论】:
-
请提供示例代码,它还会生成一些我们可以测试的示例文件。
-
按照 Komarov 的建议,我用示例代码更新了问题。
-
我在MATLAB documentation on
freadfor a precision ofint24中找不到任何参考资料,也许这就是原因? AFAIK MATLAB 仅提供有符号和无符号的int8、int16、int32和int64,当然您可以反转字节顺序。您可以尝试改为阅读int8的三元组吗? -
@MarkMikofski 在文档中实际上是
bitn。如果你运行代码,它会抛出一个警告。
标签: matlab