Memory-Mapped File Test

Windows specific questions.
Post Reply
MichaelW
Posts: 3500
Joined: May 16, 2006 22:34
Location: USA

Memory-Mapped File Test

Post by MichaelW »

Under Windows memory-mapped files are easy to set up and use, and when reading a file in small chunks the read speeds are much higher than they would be for conventional file I/O. This is the app I used to prep the test files:

Code: Select all

kill "file1.bin"
kill "file2.bin"
open "file1.bin" for binary as 1
open "file2.bin" for binary as 2
for i as integer = 0 to 9999999
    put #1,,i
    put #2,,i
next
close
print "done"
sleep
And the app I used to test the read speeds:

Code: Select all

'====================================================================
#include "windows.bi"
'====================================================================

#define SHOW_VALUES

dim as HANDLE hFile2, hMap
dim as uinteger ptr pMem
dim as double t

open "file1.bin" for binary as 1

'---------------------------------------------------------------
'' Allow time for the system processes involved in starting our
'' application to complete, reducing the interruptions in our
'' timing code and improving the run to run consistency of the
'' timings.
'---------------------------------------------------------------

sleep 3000

'--------------------------------------------------------------------
'' Using the API, open file2.bin and save the file handle, create an
'' unnamed file mapping object and save the handle, map a view of the
'' entire file into our address space and save the returned pointer,
'' and time the entire process to demonstrate that there is no long
'' delay involved.
'--------------------------------------------------------------------

t = timer

hFile2 = CreateFile( "file2.bin", _
                     GENERIC_READ or GENERIC_WRITE, _
                     FILE_SHARE_READ or FILE_SHARE_WRITE, _
                     NULL, _
                     OPEN_EXISTING, _
                     FILE_ATTRIBUTE_NORMAL, _
                     NULL )

hMap = CreateFileMapping( hFile2, NULL, PAGE_READWRITE, 0 ,0, NULL )


pMem = MapViewOfFile( hMap, FILE_MAP_WRITE, 0, 0, 0 )

print hex(pMem);"h"

print using "##.#### seconds"; timer - t

'====================================================================

'---------------------------------------------------------
'' Do a timed read of each of the files and conditionally
'' display the first and last 32-bit values.
'---------------------------------------------------------

dim as integer buf

'-------------------------------------------

t = timer

for i as integer = 0 to 9999999
    get #1,, buf
  #ifdef SHOW_VALUES
    if i = 0 or i = 9999999 then print buf
  #endif
next

print using "##.## seconds"; timer - t

'-------------------------------------------

t = timer

for i as integer = 0 to 9999999
    buf = *(pMem+i)
  #ifdef SHOW_VALUES
    if i = 0 or i = 9999999 then print buf
  #endif
next

print using "##.## seconds"; timer - t

'-------------------------------------------

sleep
And my results with SHOW_VALUES defined (9.6:1):

Code: Select all

7C0000h
 0.0015 seconds
 0
 9999999
 8.92 seconds
 0
 9999999
 0.93 seconds
And my results without SHOW_VALUES defined (24.2:1):

Code: Select all

7C0000h
 0.0016 seconds
 8.47 seconds
 0.35 seconds
Post Reply