gpt4 book ai didi

linux - 出于好奇 : how come linux kernel "optimized" strcpy is much slower the libc imp?

转载 作者:IT王子 更新时间:2023-10-29 00:37:06 34 4
gpt4 key购买 nike

我尝试在 http://lxr.linux.no/#linux+v2.6.38/arch/x86/lib/string_32.c 下对优化的字符串操作进行基准测试并与常规 strcpy 进行比较:

#include<stdio.h>
#include<stdlib.h>
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

为什么?

最佳答案

如果您的字符串是常量,则编译器可能正在内联副本(对于普通 strcpy 调用),使其成为一系列无条件 MOV 指令。因为这是没有条件的线性代码,所以它会比 linux 变体更快。

关于linux - 出于好奇 : how come linux kernel "optimized" strcpy is much slower the libc imp?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5927719/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com