23.06.2015 Views

MATLAB Programming

MATLAB Programming

MATLAB Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Accessing Files with Memory-Mapping<br />

Benefits of Memory-Mapping<br />

The principal benefits of memory-mapping are efficiency, faster file access, the<br />

ability to share memory between applications, and more efficient coding.<br />

Faster File Access. Accessing files via memory map is faster than using I/O<br />

functions such as fread and fwrite. Data is read and written using the<br />

virtual memory capabilities that are built in to the operating system rather<br />

than having to allocate, copy into, and then deallocate data buffers owned by<br />

the process.<br />

<strong>MATLAB</strong> does not access data from the disk when the map is first constructed.<br />

It only reads or writes the file on disk when a specified part of the memory<br />

map is accessed, and then it only reads that specific part. This provides faster<br />

random access to the mapped data.<br />

Efficiency. Mapping a file into memory allows access to data in the file as<br />

if that data had been read into an array in the application’s address space.<br />

Initially, <strong>MATLAB</strong> only allocates address space for the array; it does not<br />

actually read data from the file until you access the mapped region. As a<br />

result, memory-mapped files provide a mechanism by which applications can<br />

access data segments in an extremely large file without having to read the<br />

entire file into memory first.<br />

Efficient Coding Style. Memory-mapping eliminates the need for explicit<br />

calls to the fread and fwrite functions. In <strong>MATLAB</strong>, if x is a memory-mapped<br />

variable, and y is the data to be written to a file, then writing to the file is<br />

as simple as<br />

x.Data = y;<br />

6-35

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!