我发现使用 Matlab 'fread' 和 'int24' 选项读取以 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;
Run Code Online (Sandbox Code Playgroud)
输出显示:经过的时间为 1.066489 秒。已用时间为 0.047944 秒。
虽然“int32”版本读取更多数据(32*n 位),但它比“int24”读取快 25 倍。
| 归档时间: |
|
| 查看次数: |
1700 次 |
| 最近记录: |