// FileMapTest.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "FileMapTest.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
#include "MtVerify.h"
// The one and only application object
CWinApp theApp;
using namespace std;
void ReadFileWithFileMap()
{
DWORD dwStart = GetTickCount();
HANDLE hLargeFile = CreateFile("C://LargeFile.txt", GENERIC_READ,
FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
MTVERIFY(hLargeFile != NULL);
HANDLE hFileMap = CreateFileMapping(hLargeFile, NULL, PAGE_READONLY,
0, 0, NULL);
MTVERIFY(hFileMap != NULL);
SYSTEM_INFO SysInfo;
GetSystemInfo(&SysInfo);
DWORD dwGran = SysInfo.dwAllocationGranularity;
DWORD dwFileSizeHigh;
__int64 qwFileSize = GetFileSize(hLargeFile, &dwFileSizeHigh);
qwFileSize |= (((_int64)dwFileSizeHigh) << 32);
CloseHandle(hLargeFile);
DWORD dwBlockBytes = dwGran * 1000;
if (dwBlockBytes > qwFileSize)
dwBlockBytes = (DWORD)qwFileSize;
printf("SysInfo.dwAllocationGranularity = %d, FileSize = %d, BlockBytes = %u/n",
dwGran, qwFileSize, dwBlockBytes);
TCHAR *lpbMapAddress = (TCHAR *)MapViewOfFile(hFileMap,
FILE_MAP_READ, 0, 0, dwBlockBytes);
MTVERIFY(lpbMapAddress != NULL);
//2007-08-17,09:45:13,20484.000000,20520.000000,20484.000000,20517.000000
TCHAR *pStart = strstr(lpbMapAddress, "2007-08-17");
TCHAR szData[1024];
if (pStart)
{
TCHAR *pEnd = strchr(pStart, '/n');
if (pEnd)
{
int nSize = pEnd - pStart;
if (nSize > 1024) nSize = 1024;
strncpy(szData, pStart, nSize);
szData[nSize-1] = '/0';
printf("Found data: %s/n", szData);
}
}
UnmapViewOfFile(lpbMapAddress);
CloseHandle(hFileMap);
DWORD dwMs = GetTickCount() - dwStart;
printf("Map test done, elapsed time = %d ms./n", dwMs);
}
void ReadFileByLine()
{
CStdioFile file;
CString str;
DWORD dwStart = GetTickCount();
if (file.Open("C://LargeFile.txt", CStdioFile::modeRead))
{
while(file.ReadString(str))
{
if(strncmp(str.GetBuffer(0), "2007-08-17", 10) == 0)
{
printf("Found data: %s/n", str.GetBuffer(0));
break;
}
}
file.Close();
}
DWORD dwMs = GetTickCount() - dwStart;
printf("Line-read test done, elapsed time = %d ms./n", dwMs);
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: MFC initialization failed/n"));
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
ReadFileWithFileMap(); //120 - 140 MS
printf("/n");
ReadFileByLine(); //2000+ MS
}
return nRetCode;
}
<script src="http://shots.snap.com//client/inject.js?site_name=0" type="text/javascript"></script><script src="http://shots.snap.com//client/inject.js?site_name=0" type="text/javascript"></script>
//
#include "stdafx.h"
#include "FileMapTest.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
#include "MtVerify.h"
// The one and only application object
CWinApp theApp;
using namespace std;
void ReadFileWithFileMap()
{
DWORD dwStart = GetTickCount();
HANDLE hLargeFile = CreateFile("C://LargeFile.txt", GENERIC_READ,
FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
MTVERIFY(hLargeFile != NULL);
HANDLE hFileMap = CreateFileMapping(hLargeFile, NULL, PAGE_READONLY,
0, 0, NULL);
MTVERIFY(hFileMap != NULL);
SYSTEM_INFO SysInfo;
GetSystemInfo(&SysInfo);
DWORD dwGran = SysInfo.dwAllocationGranularity;
DWORD dwFileSizeHigh;
__int64 qwFileSize = GetFileSize(hLargeFile, &dwFileSizeHigh);
qwFileSize |= (((_int64)dwFileSizeHigh) << 32);
CloseHandle(hLargeFile);
DWORD dwBlockBytes = dwGran * 1000;
if (dwBlockBytes > qwFileSize)
dwBlockBytes = (DWORD)qwFileSize;
printf("SysInfo.dwAllocationGranularity = %d, FileSize = %d, BlockBytes = %u/n",
dwGran, qwFileSize, dwBlockBytes);
TCHAR *lpbMapAddress = (TCHAR *)MapViewOfFile(hFileMap,
FILE_MAP_READ, 0, 0, dwBlockBytes);
MTVERIFY(lpbMapAddress != NULL);
//2007-08-17,09:45:13,20484.000000,20520.000000,20484.000000,20517.000000
TCHAR *pStart = strstr(lpbMapAddress, "2007-08-17");
TCHAR szData[1024];
if (pStart)
{
TCHAR *pEnd = strchr(pStart, '/n');
if (pEnd)
{
int nSize = pEnd - pStart;
if (nSize > 1024) nSize = 1024;
strncpy(szData, pStart, nSize);
szData[nSize-1] = '/0';
printf("Found data: %s/n", szData);
}
}
UnmapViewOfFile(lpbMapAddress);
CloseHandle(hFileMap);
DWORD dwMs = GetTickCount() - dwStart;
printf("Map test done, elapsed time = %d ms./n", dwMs);
}
void ReadFileByLine()
{
CStdioFile file;
CString str;
DWORD dwStart = GetTickCount();
if (file.Open("C://LargeFile.txt", CStdioFile::modeRead))
{
while(file.ReadString(str))
{
if(strncmp(str.GetBuffer(0), "2007-08-17", 10) == 0)
{
printf("Found data: %s/n", str.GetBuffer(0));
break;
}
}
file.Close();
}
DWORD dwMs = GetTickCount() - dwStart;
printf("Line-read test done, elapsed time = %d ms./n", dwMs);
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: MFC initialization failed/n"));
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
ReadFileWithFileMap(); //120 - 140 MS
printf("/n");
ReadFileByLine(); //2000+ MS
}
return nRetCode;
}
<script src="http://shots.snap.com//client/inject.js?site_name=0" type="text/javascript"></script><script src="http://shots.snap.com//client/inject.js?site_name=0" type="text/javascript"></script>