[bo]以下是引用 [un]StarWing83[/un] 在 2008-5-14 19:52 的发言:[/bo]
加快速度?很简单嘛,直接当作int,一次复制四个字节,速度自然快了……
加快速度?很简单嘛,直接当作int,一次复制四个字节,速度自然快了……
Bingo!答案就这么简单
" border="0" />[color=white]
#include <fstream> #include<ctime> #include<cassert> #include<cstring> using namespace std; char* _strcpy(char* target,const char * source) { assert(target!=NULL&&source!=NULL); char* str= target; while(*target++=*source++); return str; } char* strcpy_(char* target,const char* source) { assert(target!=NULL&&source!=NULL); int n = strlen(source); char* str=target; const char* end_source = source+n-1; char* end_target = target+n; *end_target-- = '\0'; while(source<end_source) { *target++ = *source++; *end_target-- = *end_source--; } if(n&1) *target++=*source,*target=*(end_source+1); return str; } ofstream out("lov.out"); int main(void) { char a[100000]; char b[100000]; b[100000-1]='\0'; for(int m=0;m<10;++m) { out<<"\n\n"; out<<"_strcpy(): \n"; clock_t t_=clock(); for(int k=0;k<10000;++k) strcpy_(a,b); out<<clock()-t_<<" ms"<<endl; out<<"strcpy_(): \n"; clock_t t=clock(); for(int j=0;j<10000;++j) _strcpy(a,b); out<<clock()-t<<" ms"<<endl; } out.close(); return 0; }