我正在尝试通过使用共享内存进行通信来改进我的多进程应用程序。我正在通过简单的测试进行一些分析,结果出现了一些奇怪的东西。当我试图复制存储在 SharedMemory 中的数据时,使用 ReadProcessMemory 比使用 Memcopy 更快。
我知道我不应该那样使用 SharedMemory(最好直接在共享内存中读取),但我仍然想知道为什么会这样。通过进一步调查,另一件事出现了:如果我在同一个共享内存区域(实际上是同一个区域)上连续执行 2 次 memcpy,第二个副本比第一个快两倍。
这是显示问题的示例代码。在此示例中,只有一个进程,但问题仍然存在。从共享内存区域执行 memcpy 比在我自己的进程上执行同一区域的 ReadProcessMemory 慢!
#include <tchar.h>
#include <basetsd.h>
#include <iostream>
#include <boost/interprocess/mapped_region.hpp>
#include <boost/interprocess/windows_shared_memory.hpp>
#include <time.h>
namespace bip = boost::interprocess;
#include <boost/asio.hpp>
bip::windows_shared_memory* AllocateSharedMemory(UINT32 a_UI32_Size)
{
bip::windows_shared_memory* l_pShm = new bip::windows_shared_memory (bip::create_only, "Global\\testSharedMemory", bip::read_write, a_UI32_Size);
bip::mapped_region l_region(*l_pShm, bip::read_write);
std::memset(l_region.get_address(), 1, l_region.get_size());
return l_pShm;
}
//Copy the shared memory with memcpy
void CopySharedMemory(UINT32 a_UI32_Size)
{
bip::windows_shared_memory m_shm(bip::open_only, "Global\\testSharedMemory", bip::read_only);
bip::mapped_region l_region(m_shm, bip::read_only);
void* l_pData = malloc(a_UI32_Size);
memcpy(l_pData, l_region.get_address(), a_UI32_Size);
free(l_pData);
}
//Copy the shared memory with ReadProcessMemory
void ProcessCopySharedMemory(UINT32 a_UI32_Size)
{
bip::windows_shared_memory m_shm(bip::open_only, "Global\\testSharedMemory", bip::read_only);
bip::mapped_region l_region(m_shm, bip::read_only);
void* l_pData = malloc(a_UI32_Size);
HANDLE hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE,(DWORD) GetCurrentProcessId());
size_t l_szt_CurRemote_Readsize;
ReadProcessMemory(hProcess,
(LPCVOID)((void*)l_region.get_address()),
l_pData,
a_UI32_Size,
(SIZE_T*)&l_szt_CurRemote_Readsize);
free(l_pData);
}
// do 2 memcpy on the same shared memory
void CopySharedMemory2(UINT32 a_UI32_Size)
{
bip::windows_shared_memory m_shm(bip::open_only, "Global\\testSharedMemory", bip::read_only);
bip::mapped_region l_region(m_shm, bip::read_only);
clock_t begin = clock();
void* l_pData = malloc(a_UI32_Size);
memcpy(l_pData, l_region.get_address(), a_UI32_Size);
clock_t end = clock();
std::cout << "FirstCopy: " << (end - begin) * 1000 / CLOCKS_PER_SEC << " ms" << std::endl;
free(l_pData);
begin = clock();
l_pData = malloc(a_UI32_Size);
memcpy(l_pData, l_region.get_address(), a_UI32_Size);
end = clock();
std::cout << "SecondCopy: " << (end - begin) * 1000 / CLOCKS_PER_SEC << " ms" << std::endl;
free(l_pData);
}
int _tmain(int argc, _TCHAR* argv[])
{
UINT32 l_UI32_Size = 1048576000;
bip::windows_shared_memory* l_pShm = AllocateSharedMemory(l_UI32_Size);
clock_t begin = clock();
for (int i=0; i<10 ; i++)
CopySharedMemory(l_UI32_Size);
clock_t end = clock();
std::cout << "MemCopy: " << (end - begin) * 1000 / CLOCKS_PER_SEC << " ms" << std::endl;
begin = clock();
for (int i=0; i<10 ; i++)
ProcessCopySharedMemory(l_UI32_Size);
end = clock();
std::cout << "ReadProcessMemory: " << (end - begin) * 1000 / CLOCKS_PER_SEC << " ms" << std::endl;
for (int i=0; i<10 ; i++)
CopySharedMemory2(l_UI32_Size);
delete l_pShm;
return 0;
}
这是输出:
MemCopy: 8891 ms
ReadProcessMemory: 6068 ms
FirstCopy: 796 ms
SecondCopy: 327 ms
FirstCopy: 795 ms
SecondCopy: 328 ms
FirstCopy: 780 ms
SecondCopy: 344 ms
FirstCopy: 780 ms
SecondCopy: 343 ms
FirstCopy: 780 ms
SecondCopy: 327 ms
FirstCopy: 795 ms
SecondCopy: 343 ms
FirstCopy: 780 ms
SecondCopy: 344 ms
FirstCopy: 796 ms
SecondCopy: 343 ms
FirstCopy: 796 ms
SecondCopy: 327 ms
FirstCopy: 780 ms
SecondCopy: 328 ms
如果有人知道为什么 memcpy 这么慢,如果有解决这个问题的方法,我会全神贯注。
谢谢。