Why does using memory-mapped files appear to be slower than using standard I/O functions in MATLAB?
14 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
MathWorks Support Team
le 16 Sep 2013
Modifié(e) : MathWorks Support Team
le 13 Déc 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 );
Réponse acceptée
MathWorks Support Team
le 10 Déc 2021
Modifié(e) : MathWorks Support Team
le 13 Déc 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 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Audio and Video Data dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!