0

我一直在使用 memcmp 函数在我的性能关键应用程序中比较 2 个整数。我不得不使用它而不是使用相等的运算符,因为我必须一般地处理其他数据类型。但是,我怀疑原始数据类型的 memcpy 性能并将其更改为等于运算符。但是,性能有所提高。

我只是做了一些简单的测试,如下所示。

使用 memcmp

#include <time.h>
#include <sys/time.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string.h>

using namespace std;

int main(int argc, char **argv)
{
        int iValue1 = atoi(argv[1]);
        int iValue2 = atoi(argv[2]);

        struct timeval start;
        gettimeofday(&start, NULL);

        for (int i = 0; i < 2000000000; i++)
        {
//              if (iValue1 == iValue2)
                if (memcmp(&iValue1, &iValue2, sizeof(int)) == 0)
                {
                        cout << "Hello" << endl;
                };
        };

        struct timeval end;
        gettimeofday(&end, NULL);

        cout << "Time taken : " << ((end.tv_sec * 1000000 + end.tv_usec) - (start.tv_sec * 1000000 + start.tv_usec)) << " us" << endl;
        return 0;
}

该程序的输出如下。

sujith@linux-1xs7:~> g++ -m64 -O3 Main.cpp
sujith@linux-1xs7:~> ./a.out 3424 234
Time taken : 13539618 us
sujith@linux-1xs7:~> ./a.out 3424 234
Time taken : 13534932 us
sujith@linux-1xs7:~> ./a.out 3424 234
Time taken : 13599818 us
sujith@linux-1xs7:~> ./a.out 3424 234
Time taken : 13639394 us

使用相等运算符

#include <time.h>
#include <sys/time.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string.h>

using namespace std;

int main(int argc, char **argv)
{
        int iValue1 = atoi(argv[1]);
        int iValue2 = atoi(argv[2]);

        struct timeval start;
        gettimeofday(&start, NULL);

        for (int i = 0; i < 2000000000; i++)
        {
                if (iValue1 == iValue2)
//              if (memcmp(&iValue1, &iValue2, sizeof(int)) == 0)
                {
                        cout << "Hello" << endl;
                };
        };

        struct timeval end;
        gettimeofday(&end, NULL);

        cout << "Time taken : " << ((end.tv_sec * 1000000 + end.tv_usec) - (start.tv_sec * 1000000 + start.tv_usec)) << " us" << endl;
        return 0;
}

该程序的输出如下。

sujith@linux-1xs7:~> g++ -m64 -O3 Main.cpp
sujith@linux-1xs7:~> ./a.out 234 23423
Time taken : 9 us
sujith@linux-1xs7:~> ./a.out 234 23423
Time taken : 13 us
sujith@linux-1xs7:~> ./a.out 234 23423
Time taken : 14 us
sujith@linux-1xs7:~> ./a.out 234 23423
Time taken : 15 us
sujith@linux-1xs7:~> ./a.out 234 23423
Time taken : 16 us

有人可以让我知道对于原始数据类型,等号运算符是否比 memcmp 更快?如果是这样,那里发生了什么?等号运算符不是在里面使用 memcmp 吗?

4

1 回答 1

7

微基准很难编写。

第一种情况下的循环编译为 (at g++ -O3):

    movl    $2000000000, %ebx
    jmp .L3
.L2:
    subl    $1, %ebx
    je  .L7
.L3:
    leaq    12(%rsp), %rsi
    leaq    8(%rsp), %rdi
    movl    $4, %edx
    call    memcmp
    testl   %eax, %eax
    jne .L2
    ; code to do the printing omitted
    subl    $1, %ebx
    jne .L3
.L7:
    addq    $16, %rsp
    xorl    %eax, %eax
    popq    %rbx
    ret

第二种情况下的循环编译为

    cmpl    %eax, %ebp
    je  .L7
.L2:
    addq    $8, %rsp
    xorl    %eax, %eax
    popq    %rbx
    popq    %rbp
    ret
.L7:
    movl    $2000000000, %ebx
.L3:
    ; code to do the printing omitted
    subl    $1, %ebx
    jne .L3
    jmp .L2

请注意,在第一种情况下memcmp被称为 2000000000 次。在第二种情况下,优化器将比较提升到了循环之外,所以它只进行了一次。此外,在第二种情况下,编译器将这两个变量完全放在寄存器中,而在第一种情况下,它们需要放在堆栈中,因为您正在获取它们的地址。

即使只看比较,比较两个ints 也需要一条cmpl指令。使用memcmp会导致函数调用,并且在内部memcmp可能需要一些额外的检查。

在这种特殊情况下,clang++ -O3将 编译memcmp为单个cmpl指令。但是,如果您使用memcmp.

于 2014-07-20T14:40:42.820 回答