关于内存映射的基本知识以及一些函数的原型说明,参考博客:http://blog.csdn.net/wcyoot/article/details/7363393
下面是我对于读取一个104M文件大小,使用内存映射和传统的读文件方法进行了时间上的对比,两种方法都从该文讲读取300000*34个浮点数。
测试环境:vs2010
#include<iostream>
#include <cstringt.h>
#include<vector>
#include<windows.h>
#include <ctime>
using namespace std; int main()
{
clock_t start, finish;
//------------------------------------------------------内存映射方法;
start = clock();
//创建或打开文件内核对象;
HANDLE fileH = CreateFile("ldFeature.txt",
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if(fileH == INVALID_HANDLE_VALUE)
{
cout<<"error in CreateFile"<<endl;
return -;
} //创建一个文件映射内核对象;
HANDLE mapFileH = CreateFileMapping( fileH,
NULL,
PAGE_READWRITE,
,
,
"Resource " );
if(mapFileH == NULL)
{
cout<<"error in CreateFileMapping"<<endl;
return -;
} //将文件数据映射到进程的地址空间;
char * mapH = (char *)MapViewOfFile( mapFileH,
FILE_MAP_ALL_ACCESS,
,
,
);
if(mapH == NULL)
{
cout<<"error in MapViewOfFile"<<endl;
return -;
} //读取数据;
char *buf = mapH;
double k;
int times = *;
for(int i = ; i <= times; i++)
{
k = atof(buf);
buf = strstr(buf+," ");
//cout<<k<<endl;
}
//关闭句柄;
UnmapViewOfFile(mapH);
CloseHandle(mapFileH);
CloseHandle(fileH); finish = clock();
//打印耗时;
cout<<"time:"<<(double)(finish - start)/ CLOCKS_PER_SEC<<endl; //------------------------------------------------------传统方法;
start = clock(); FILE *fp = fopen("ldFeature.txt","r");
if(fp == NULL)
{
cout<<"error in fopen"<<endl;
return -;
}
for(int i = ;i <= times; i++)
{
fscanf(fp,"%lf",&k);
}
fclose(fp); finish = clock();
cout<<"time:"<<(double)(finish - start)/ CLOCKS_PER_SEC<<endl; return ;
}
结果显示,传统方法耗时是内存映射方法的2倍左右。实际上文件越大,使用内存映射方法的优势越明显:
【版权声明】转载请注明出处 http://www.cnblogs.com/TenosDoIt/p/3157596.html