Use memory-mapped file to improve the performance you

Author:Anonymous    Updated:2008-3-20 23:18:26
I study the "core programming WINDOWS" time to master JEFFREY mentioned a small program written to compare the performance of two versions, the program's original demand is this: a large file on the reverse, that is, will be a document head into tail, the end of a first.
The methods used there are many, where the use of two methods of comparison, the main highlight is the use of memory-mapped file benefits two ways: memory mapping file, I / O read and write caching approach.

The first one is to create memory-mapped file object, and then the object is mapped to the address space in the process, then read the contents of the documents, and then reverse, then writes the document.

In the second method is to read the contents of the documents would be a big buffer, and then reverse, then writes the document, among the original documents to delete, then re-write.

Prepared by the following procedure

The first method, the memory mapping file:


BOOL FileReverse (PCTSTR pszPathName)
(
HANDLE hFile = CreateFile (pszPathName, GENERIC_WRITE | GENERIC_READ, 0, NULL
, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
If (hFile == INVALID_HANDLE_VALUE)
(
Printf ( "File could not be opened.");
Return FALSE;
)

DWORD dwFileSize = GetFileSize (hFile, NULL);

HANDLE hFileMap = CreateFileMapping (hFile, NULL, PAGE_READWRITE, 0,
DwFileSize + sizeof (char), NULL);

If (hFileMap == NULL) (
CloseHandle (hFile);
Return FALSE;
)

PVOID pvFile = MapViewOfFile (hFileMap, FILE_MAP_WRITE, 0,0,0);

If (pvFile == NULL) (
CloseHandle (hFileMap);
CloseHandle (hFile);
Return FALSE;
)

PSTR pchAnsi = (PSTR) pvFile;
PchAnsi [dwFileSize / sizeof (char)] = 0;
_strrev (PchAnsi);

PchAnsi = strchr (pchAnsi, '\ n');
While (pchAnsi! = NULL) (
* PchAnsi + + = '\ r';
* PchAnsi + + = '\ n';
PchAnsi = strchr (pchAnsi, '\ n');
)

UnmapViewOfFile (pvFile);
CloseHandle (hFileMap);

SetFilePointer (hFile, dwFileSize, NULL, FILE_BEGIN);
SetEndOfFile (hFile); / / do not actually written into the.
CloseHandle (hFile);

Return TRUE;
)


In the second method, the use of cache:

BOOL FileReverseNoMap (PCTSTR pszPathName)
(
HANDLE hFile = CreateFile (pszPathName, GENERIC_WRITE | GENERIC_READ, 0, NULL
, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
If (hFile == INVALID_HANDLE_VALUE)
(
Printf ( "File could not be opened.");
Return FALSE;
)

DWORD dwFileSize = GetFileSize (hFile, NULL);
/ / CloseHandle (hFile);
Char * readBuf = new char [dwFileSize +1];
DWORD nRead = 0, nRet = 0;
While (nRead <dwFileSize) (
If (ReadFile (hFile, readBuf + nRead, dwFileSize-nRead, & nRet, NULL) == TRUE)
(
NRead + = nRet;
)
Else
(
Printf ( "Can read the file!");
CloseHandle (hFile);
)
)

PSTR pchAnsi = (PSTR) readBuf;
PchAnsi [dwFileSize / sizeof (char)] = 0;
_strrev (PchAnsi);

PchAnsi = strchr (pchAnsi, '\ n');
While (pchAnsi! = NULL) (
* PchAnsi + + = '\ r';
* PchAnsi + + = '\ n';
PchAnsi = strchr (pchAnsi, '\ n');
)
CloseHandle (hFile);
DeleteFile (pszPathName);

HANDLE hWriteFile = CreateFile (pszPathName, GENERIC_WRITE | GENERIC_READ, 0, NULL
, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
WriteFile (hWriteFile, readBuf, dwFileSize, & nRet, NULL);
CloseHandle (hWriteFile);

Delete readBuf;

Return TRUE;
)


I run several times, the results are as follows:
File Size (byte) 1 Method time (ms) 2 Method time (ms)
25416 0 0
101664 0 0
406656 0 10
1219968 10 30
3202416 21 100
9607248 80 551
67250736 581 5568

I tested the machine is fast CPU pool 1.5 notebook, the memory of 712 MB

Through the above we can see that the use of testing memory mapping document the benefits of greater memory in this document reflect on the advantages of the more obvious, the main reasons are:

Memory-mapped file documents directly mapped to the address of the process address space, then operate on the equivalent document in memory operation, eliminating the read and write I / O time, the second way is to do so (READFILE , WRITEFILE), this process is very slow.
Previous:Read and write a simple example of the registry
Next:MFC image with the news of dynamic menu
User Reviews
Related Articles
Recommended article
AD