64,654
社区成员
发帖
与我相关
我的任务
分享
void* Pz_memcpy(void* dest, void* src, size_t len)
{
size_t l = len / 8;
size_t m = len % 8;
int* si = (int*)src;
int* di = (int*)dest;
while (l--)
{
*di++ = *si++;
*di++ = *si++;
}
unsigned char* sc = (unsigned char*)si;
unsigned char* dc = (unsigned char*)di;
switch(m)
{
case 8: *dc++ = *sc++;
case 7: *dc++ = *sc++;
case 6: *dc++ = *sc++;
case 5: *dc++ = *sc++;
case 4: *dc++ = *sc++;
case 3: *dc++ = *sc++;
case 2: *dc++ = *sc++;
case 1: *dc++ = *sc++;
case 0:
default:
break;
}
return dest;
}
case 0:
default:
break;
//test.cpp
//memcpy test
//编译命令:cl /EHsc /O2 test.cpp memcpy.cpp
namespace squall
{
namespace common
{
void* asmcpy1(void* dst, void const* src, unsigned int countbytes);
void* asmcpy2(void* dst, void const* src, unsigned int countbytes);
void* simcpy1(void* dst, void const* src, unsigned int countbytes);
void* simcpy2(void* dst, void const* src, unsigned int countbytes);
void* simcpy3(void* dst, void const* src, unsigned int countbytes);
void* simcpy4(void* dst, void const* src, unsigned int countbytes);
}
}
#include <iostream>
#include <cstdlib>
#include <ctime>
using std::endl;
using std::cout;
int main()
{
int const count = 1000000;
int const bufsize = 2048;
int const maxindex1 = 4;
int const maxindex2 = 4;
static char dstbuf[bufsize+maxindex2];
static char srcbuf[bufsize+maxindex2];
clock_t start, end, sum;
cout << "函数名称\t执行时间\tDst偏移\tSrc偏移\t数据量\t循环次数\n";
sum = 0;
for(int dstindex=0; dstindex<maxindex1; dstindex++)
{
for(int srcindex=0; srcindex<maxindex2; srcindex++)
{
char* dst = &dstbuf[dstindex];
char* src = &srcbuf[srcindex];
start = clock();
for(int i=0; i<count; i++) std::memcpy(dst, src, bufsize);
end = clock();
cout << "memcpy()\t" << end-start << "\t\t" << dstindex << '\t' << srcindex << '\t' << bufsize << '\t' << count << '\n';
sum += (end-start);
}
}
cout << "平均值\t\t" << static_cast<float>(sum)/(maxindex1*maxindex2) << endl << endl;
sum = 0;
for(int dstindex=0; dstindex<maxindex1; dstindex++)
{
for(int srcindex=0; srcindex<maxindex2; srcindex++)
{
char* dst = &dstbuf[dstindex];
char* src = &srcbuf[srcindex];
start = clock();
for(int i=0; i<count; i++) squall::common::asmcpy2(dst, src, bufsize);
end = clock();
cout << "asmcpy2()\t" << end-start << "\t\t" << dstindex << '\t' << srcindex << '\t' << bufsize << '\t' << count << '\n';
sum += (end-start);
}
}
cout << "平均值\t\t" << static_cast<float>(sum)/(maxindex1*maxindex2) << endl << endl;
sum = 0;
for(int dstindex=0; dstindex<maxindex1; dstindex++)
{
for(int srcindex=0; srcindex<maxindex2; srcindex++)
{
char* dst = &dstbuf[dstindex];
char* src = &srcbuf[srcindex];
start = clock();
for(int i=0; i<count; i++) squall::common::simcpy4(dst, src, bufsize);
end = clock();
cout << "simcpy4()\t" << end-start << "\t\t" << dstindex << '\t' << srcindex << '\t' << bufsize << '\t' << count << '\n';
sum += end-start;
}
}
cout << "平均值\t\t" << static_cast<float>(sum)/(maxindex1*maxindex2) << endl << endl;
sum = 0;
for(int dstindex=0; dstindex<maxindex1; dstindex++)
{
for(int srcindex=0; srcindex<maxindex2; srcindex++)
{
char* dst = &dstbuf[dstindex];
char* src = &srcbuf[srcindex];
start = clock();
for(int i=0; i<count; i++) squall::common::simcpy3(dst, src, bufsize);
end = clock();
cout << "simcpy3()\t" << end-start << "\t\t" << dstindex << '\t' << srcindex << '\t' << bufsize << '\t' << count << '\n';
sum += end-start;
}
}
cout << "平均值\t\t" << static_cast<float>(sum)/(maxindex1*maxindex2) << endl << endl;
sum = 0;
for(int dstindex=0; dstindex<maxindex1; dstindex++)
{
for(int srcindex=0; srcindex<maxindex2; srcindex++)
{
char* dst = &dstbuf[dstindex];
char* src = &srcbuf[srcindex];
start = clock();
for(int i=0; i<count; i++) squall::common::simcpy2(dst, src, bufsize);
end = clock();
cout << "simcpy2()\t" << end-start << "\t\t" << dstindex << '\t' << srcindex << '\t' << bufsize << '\t' << count << '\n';
sum += end-start;
}
}
cout << "平均值\t\t" << static_cast<float>(sum)/(maxindex1*maxindex2) << endl << endl;
sum = 0;
for(int dstindex=0; dstindex<maxindex1; dstindex++)
{
for(int srcindex=0; srcindex<maxindex2; srcindex++)
{
char* dst = &dstbuf[dstindex];
char* src = &srcbuf[srcindex];
start = clock();
for(int i=0; i<count; i++) squall::common::simcpy1(dst, src, bufsize);
end = clock();
cout << "simcpy1()\t" << end-start << "\t\t" << dstindex << '\t' << srcindex << '\t' << bufsize << '\t' << count << '\n';
sum += end-start;
}
}
cout << "平均值\t\t" << static_cast<float>(sum)/(maxindex1*maxindex2) << endl << endl;
return 0;
}