Why does using memory-mapped files appear to be slower than using standard I/O functions in MATLAB?

14 visualizaciones (últimos 30 días)
The documentation for MATLAB claims that accessing memory-mapped files is faster than using standard I/O function like FREAD and FWRITE. When I write code using the MEMMAPFILE object, similar to the following,
m = memmapfile ('myfile.dat', 'Format', 'double' );
for i = 1 : N
y(i) = m.Data(i);
end
I expect the above code to run faster than the following equivalent code:
fid = fopen ('myfile.dat', 'r' );
for i = 1 : N
y(i) = fread ( fid, 1, 'double' );
end
fclose( fid );

Respuesta aceptada

MathWorks Support Team
MathWorks Support Team el 10 de Dic. de 2021
Editada: MathWorks Support Team el 13 de Dic. de 2021
At the operating system level, using a memory-mapped file is faster than using standard I/O functions. However, there is some additional overhead incurred in using the MEMMAPFILE object within MATLAB.
With MEMMAPFILE, there is an overhead penalty associated with every access of the Data member of this class. Therefore you will likely not see improved performance when reading or writing a small number of values in a single assignment to a memory mapped file when compared to FWRITE.
To work around this issue, you can create a reference to the "Data" property, which cuts down on some overhead when accessing data. The following code using memory-mapped files should run faster than using standard I/O functions:
m = memmapfile ('myfile.dat', 'Format', 'double' );
dataRef = m.Data;
for k = 1 : N
y(k) = dataRef (k);
end
The general rules and techniques for vectorization of code apply even more so to MEMMAPFILE objects. You can find details regarding vectorization here:
Techniques to improve performance (MATLAB Programming):
More information on memory mapped files in MATLAB is available here:
Accessing Files with Memory-Mapping :: Data Import and Export (MATLAB Programming)

Más respuestas (0)

Productos

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by