test speed of shared memory
代码
// sharedMem.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <tchar.h>
#include <vector>
#define BUF_SIZE (8000*2000)
TCHAR szName[]=TEXT("Global\\MyFileMappingObject");
int server(char *exe)
{
HANDLE hMapFile;
LPCTSTR pBuf;
std::vector<char> vBuf(BUF_SIZE);
hMapFile = CreateFileMapping(
INVALID_HANDLE_VALUE, // use paging file
NULL, // default security
PAGE_READWRITE, // read/write access
0, // maximum object size (high-order DWORD)
BUF_SIZE, // maximum object size (low-order DWORD)
szName); // name of mapping object
if (hMapFile == NULL)
{
_tprintf(TEXT("Could not create file mapping object (%d).\n"),
GetLastError());
return 1;
}
pBuf = (LPTSTR) MapViewOfFile(hMapFile, // handle to map object
FILE_MAP_ALL_ACCESS, // read/write permission
0,
0,
BUF_SIZE);
if (pBuf == NULL)
{
_tprintf(TEXT("Could not map view of file (%d).\n"),
GetLastError());
CloseHandle(hMapFile);
return 1;
}
DWORD dwTm = GetTickCount();
CopyMemory((PVOID)pBuf, &vBuf[0], vBuf.size());
DWORD dwDiff = GetTickCount()-dwTm;
printf("Copy from user to shared memory use time %d\n", dwDiff);
char cmd[1000];
sprintf(cmd,"%s c", exe);
system(cmd);
getch();
UnmapViewOfFile(pBuf);
CloseHandle(hMapFile);
return 0;
}
int client()
{
HANDLE hMapFile;
LPCTSTR pBuf;
std::vector<char> vBuf(BUF_SIZE);
hMapFile = OpenFileMapping(
FILE_MAP_ALL_ACCESS, // read/write access
FALSE, // do not inherit the name
szName); // name of mapping object
if (hMapFile == NULL)
{
_tprintf(TEXT("Could not open file mapping object (%d).\n"),
GetLastError());
return 1;
}
pBuf = (LPTSTR) MapViewOfFile(hMapFile, // handle to map object
FILE_MAP_ALL_ACCESS, // read/write permission
0,
0,
BUF_SIZE);
if (pBuf == NULL)
{
_tprintf(TEXT("Could not map view of file (%d).\n"),
GetLastError());
CloseHandle(hMapFile);
return 1;
}
DWORD dwTm = GetTickCount();
CopyMemory(&vBuf[0], (PVOID)pBuf, vBuf.size());
DWORD dwDiff = GetTickCount()-dwTm;
printf("Copy from shared memory to user use time %d\n", dwDiff);
UnmapViewOfFile(pBuf);
CloseHandle(hMapFile);
return 0;
}
int _tmain(int argc, char *argv[])
{
if(argc>1)
{
if(argv[1][0]=='c')
client();
}
else
server(argv[0]);
return 0;
}
//
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <tchar.h>
#include <vector>
#define BUF_SIZE (8000*2000)
TCHAR szName[]=TEXT("Global\\MyFileMappingObject");
int server(char *exe)
{
HANDLE hMapFile;
LPCTSTR pBuf;
std::vector<char> vBuf(BUF_SIZE);
hMapFile = CreateFileMapping(
INVALID_HANDLE_VALUE, // use paging file
NULL, // default security
PAGE_READWRITE, // read/write access
0, // maximum object size (high-order DWORD)
BUF_SIZE, // maximum object size (low-order DWORD)
szName); // name of mapping object
if (hMapFile == NULL)
{
_tprintf(TEXT("Could not create file mapping object (%d).\n"),
GetLastError());
return 1;
}
pBuf = (LPTSTR) MapViewOfFile(hMapFile, // handle to map object
FILE_MAP_ALL_ACCESS, // read/write permission
0,
0,
BUF_SIZE);
if (pBuf == NULL)
{
_tprintf(TEXT("Could not map view of file (%d).\n"),
GetLastError());
CloseHandle(hMapFile);
return 1;
}
DWORD dwTm = GetTickCount();
CopyMemory((PVOID)pBuf, &vBuf[0], vBuf.size());
DWORD dwDiff = GetTickCount()-dwTm;
printf("Copy from user to shared memory use time %d\n", dwDiff);
char cmd[1000];
sprintf(cmd,"%s c", exe);
system(cmd);
getch();
UnmapViewOfFile(pBuf);
CloseHandle(hMapFile);
return 0;
}
int client()
{
HANDLE hMapFile;
LPCTSTR pBuf;
std::vector<char> vBuf(BUF_SIZE);
hMapFile = OpenFileMapping(
FILE_MAP_ALL_ACCESS, // read/write access
FALSE, // do not inherit the name
szName); // name of mapping object
if (hMapFile == NULL)
{
_tprintf(TEXT("Could not open file mapping object (%d).\n"),
GetLastError());
return 1;
}
pBuf = (LPTSTR) MapViewOfFile(hMapFile, // handle to map object
FILE_MAP_ALL_ACCESS, // read/write permission
0,
0,
BUF_SIZE);
if (pBuf == NULL)
{
_tprintf(TEXT("Could not map view of file (%d).\n"),
GetLastError());
CloseHandle(hMapFile);
return 1;
}
DWORD dwTm = GetTickCount();
CopyMemory(&vBuf[0], (PVOID)pBuf, vBuf.size());
DWORD dwDiff = GetTickCount()-dwTm;
printf("Copy from shared memory to user use time %d\n", dwDiff);
UnmapViewOfFile(pBuf);
CloseHandle(hMapFile);
return 0;
}
int _tmain(int argc, char *argv[])
{
if(argc>1)
{
if(argv[1][0]=='c')
client();
}
else
server(argv[0]);
return 0;
}