简体   繁体   English

只是出于好奇:Linux内核“优化”的strcpy为什么比libc imp慢得多?

[英]Just out of curiosity: how come linux kernel “optimized” strcpy is much slower the libc imp?

I tried to benchmark optimized string operations under http://lxr.linux.no/#linux+v2.6.38/arch/x86/lib/string_32.c and compare to regular strcpy: 我尝试在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;
}

and timeing it using (time .. ) showed that it is about x10 slower than regular strcpy (under x64 linux) 并使用(time ..)对其计时,表明它比常规strcpy慢x10(在x64 linux下)

Why? 为什么?

If your string is constant, it's possible that the compiler is inlining the copy (for the plain strcpy call), making it into a series of unconditional MOV instructions. 如果您的字符串是常量,则编译器可能会内联该副本(用于普通的strcpy调用),从而使其成为一系列无条件的MOV指令。 since this is linear code without conditions, it would be faster than the linux variant. 由于这是没有条件的线性代码,因此它将比linux变体更快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM