Why does using memory-mapped files appear to be slower than using standard I/O functions in MATLAB?
14 次查看(过去 30 天)
显示 更早的评论
MathWorks Support Team
2013-9-16
编辑: MathWorks Support Team
2021-12-13
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 );
采纳的回答
MathWorks Support Team
2021-12-10
编辑:MathWorks Support Team
2021-12-13
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Audio and Video Data 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!