Matlab Slowly Reads 24-Bit Integers

I found that reading data that is packed in a 24-bit integer format using Matlab 'fread' with the 'int24' option takes a lot of time. I found that if I read data in 'int32' or 'int16' or 'int8', the read time will be extremely fast compared to 'int24'. Is there a better way to reduce the time it takes to read 24-bit integer data?

To understand the essence of the problem, a code example is provided.

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;

Conclusion: The elapsed time is 1.066489 seconds. Elapsed time is 0.047944 seconds.

Although the version of "int32" reads more data (32 * n bits), it is 25 times faster than reading "int24".

+4
source share
2

4- , 8- 24- . , , , big-endian :

>> tic;
>> fp = fopen('file1.bin', 'r');
>> y1 = fread(fp, n, 'bit24');
>> fclose(fp);
>> toc;
Elapsed time is 0.593552 seconds.

>> tic;
>> fp = fopen('file1.bin', 'r');
>> y2 = double(fread(fp, n, '*uint8'));  % This is fastest, for some reason
>> y2 = [1 256 65536]*reshape([y2; zeros(3-rem(numel(y2), 3), 1)], 3, []);
>> fclose(fp);
>> toc;
Elapsed time is 0.143388 seconds.

>> isequal(y1,y2.')  % Test for equality of the values

ans =

     1

y2 , y1. y2 , , , . - fread uint8, double , (.. double 'uint8' 'uint8=>double').

+1

-, bitn int*.

int24 bit32, . , , , bitn.

0

All Articles