Why does using memory-mapped files appear to be slower than using standard I/O functions in MATLAB?
14 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
MathWorks Support Team
am 16 Sep. 2013
Bearbeitet: MathWorks Support Team
am 13 Dez. 2021
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 );
Akzeptierte Antwort
MathWorks Support Team
am 10 Dez. 2021
Bearbeitet: MathWorks Support Team
am 13 Dez. 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)
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Audio and Video Data finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!