#include
#include
char *_strcpy(char *dest, const char *src)
{
int d0, d1, d2;
asm volatile("1:\tlodsb\n\t"
"stosb\n\t"
"testb %%al,%%al\n\t"
"jne 1b"
: "=&S" (d0), "=&D" (d1), "=&a" (d2)
: "0" (src), "1" (dest) : "memory");
return dest;
}
int main(int argc, char **argv){
int times = 1;
if(argc >1)
{
times = atoi(argv[1]);
}
char a[100];
for(; times; times--)
_strcpy(a, "Hello _strcpy!");
return 0;
}
并使用(time ..)显示它比常规strcpy(在x64 linux下)慢约x10
为什么?
最佳答案 如果您的字符串是常量,则编译器可能正在内联副本(对于plain strcpy调用),使其成为一系列无条件MOV指令.
因为这是无条件的线性代码,所以它比linux变体更快.