ChangeMyName
ChangeMyName

Reputation: 7418

Read with File Mapping Objects in C++

I am trying to use Memory Mapped File (MMF) to read my .csv data file (very large and time consuming).

I've heared that MMF is very fast since it caches content of the file, thus users can get access to the content in disk as in memory.

May I know if MMF is any faster than using other reading methods?

If this is true, can anyone show me a simple example how to read a file from disk?

Many thanks in advance.

Upvotes: 0

Views: 1596

Answers (2)

Mats Petersson
Mats Petersson

Reputation: 129494

If it's faster or not depends on many different factors (such as what data you are accessing, how you are accessing it, etc. To determine what is right for YOUR case, you need to benchmark different solutions, and see what is best in your case.

The main benefit of memory mapped files is that the data can be copied directly from the filesystem into the user-accessible memory.

In traditional (fstream::read(), fredad(), etc) types of file-reading, the content of the file is read into a temporary buffer in the OS, then (part of) that buffer is copied to the user supplied buffer. This is because the OS can't rely on the memory being there and it gets pretty messy pretty quickly. For memory mapped files, the OS knows directly where the memory is for the different sections (because it's the OS's task to assign that memory and keep track of where it is!) of the file, so the OS can just copy it straight in.

However, I strongly suspect that the method of reading the file is a minor part, and the actual interpretation/parsing/copying out of the file may well be a large part. [Speculation, we haven't seen your code, of course]. And of course, the I/O speed available from the DISK itself may play a large factor if the file is very large.

Upvotes: 0

Adrian McCarthy
Adrian McCarthy

Reputation: 48021

May I know if MMF is any faster than using other reading methods?

If you're reading the entire file sequentially in one pass, then a memory-mapped file is probably approximately the same as using conventional file I/O.

can anyone show me a simple example how to read a file from disk?

Memory mapped files are typically an operating system feature, so you'd have to tell us which platform you're on to get an example of using it.

If you want to read a file sequentially, you can use the C++ ifstream class or the C run-time functions like fopen, fread, and fclose.

Upvotes: 0

Related Questions