使用以下代码比较了memcpy的效率,结果证明memcpy来拷贝内存段效率更高。
使用以下代码比较了memcpy的效率,结果证明memcpy来拷贝内存段效率更高。
#include "stdafx.h"
#include "cSRLog.h"
#include "cProfiler.h"
static const int PACKET_SIZE = 1024;
static const int BATCH_TIMES = 1000000;
int _tmain(int argc, _TCHAR* argv[])
{
new cSRLog();
BYTE *pSrc = new BYTE[PACKET_SIZE];
BYTE *pDst = new BYTE[PACKET_SIZE];
{
cProfiler pf( "Profiler1" );
for( int i=0; i memcpy( pDst, pSrc, PACKET_SIZE );
}
{
cProfiler pf( "Profiler2" );
for( int i=0; i for( int j=0; j pDst[j] = pSrc[j];
}
delete pSrc;
delete pDst;
delete cSRLog::GetPtr();
return 0;
}
PACKET_SIZE = 1024 的结果如下:
Profiler1: 321.364866 ms.
Profiler2: 3426.804803 ms.
PACKET_SIZE = 128 的结果如下:
Profiler1: 97.463504 ms.
Profiler2: 325.906226 ms.
PACKET_SIZE = 32 的结果如下:
Profiler1: 47.229746 ms.
Profiler2: 129.446899 ms.
测试环境为
P4 1.8G 512M
VisualStudio 2003
Windows XP SP2
你可以使用这个链接引用该篇文章 http://publishblog.blogchina.com/blog/tb.b?diaryID=215320