以上两楼数据是复制100M数据的结果……代码如下:
#include <iostream>
#include <cassert>
#include <ctime>
char* str_cpy(char* dest,const char* src)
{
assert(dest != NULL && src != NULL);
char *str=dest;
int *pd=(int*)dest,*ps=(int*)src;
while (*ps&0xff && *ps&0xff00 && *ps&0xff0000 && *ps&0xff000000)
*pd++=*ps++;
for (dest=(char*)pd,src=(char*)ps;*dest++=*src++;);
return str;
}
char* str_cpy2(char* dest,const char* src)
{
assert(dest != NULL && src != NULL);
char *str=dest;
while (*dest++ = *src++);
return str;
}
#define N 100000000
char a[N],s[N],d[N];
#define Test(strcpy) \
do { \
for(int i=0;i<N;i++)a[i]=s[i],d[i]=0; \
clock_t t=clock(); \
strcpy(d,a); \
printf(#strcpy" Uses time %ldms\n",clock()-t); \
} while(0)
int main(void)
{
for (int i=0;i<N;i++)s[i]=rand()%255+1;
s[N-1]=0;
Test(str_cpy);
Test(str_cpy2);
Test(strcpy);
return 0;
}