将参数传递给具有const参数的函数:更快吗? [英] Passing arguments to functions with const parameters: is it faster?

查看:196
本文介绍了将参数传递给具有const参数的函数:更快吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一下,例如:

int sum(int a, int b)
{
    return a + b;
}

vs.

int sum(const int a, const int b)
{
    return a + b;
}

第二种方法通常更快吗?

Is the second approach in general faster?

将C语言中的函数参数复制并发送给函数,以便函数内部的更改不会影响原始值.我的理由是,在上面的第二个sum中,编译器确定在函数内部未修改ab,因此它可以传递原始值而无需先复制它们.这就是为什么我认为第二个sum快于第一个sum的原因.但是我真的不知道.在上述sum的特定简单示例中,差异(如果有的话)应该很小.

Function parameters in C are copied and sent to the function, so that changes inside the function do not affect the original values. My reasoning is that in the second sum above, the compiler knows for sure that a and b are not modified inside the function, so it can just pass the original values without copying them first. That's why I think the second sum is faster than the first. But I don't really know. In the particular simple example of sum above, the differences, if any, should be minimal.

编辑:sum示例只是为了说明我的观点.我不希望在此特定示例中会有很大的差异.但是我想知道在更复杂的情况下,编译器是否可以利用函数参数中的const修饰符来使函数更快.我怀疑编译器是否总能确定函数内部是否更改了参数(因此,我在下面提出的第二个问题);因此,我希望当它找到const修饰符时,它会执行与没有const修饰符时有所不同的事情.

The sum example is just to illustrate my point. I don't expect that in this particular example there should be large differences. But I wonder if in more complicated situations the const modifier inside a function parameter can be exploited by the compiler to make the function faster. I doubt that the compiler can always determine whether a parameter is changed inside a function (hence my 2nd question below); hence I'd expect that when it finds a const modifier, it does something different than when there's no const modifier.

问题:一般来说,当函数的参数为​​const时,它的速度要比非参数的更快.

Question: In general, a function will be faster when its arguments are const, than when they are not?

问题2:通常,C编译器是否可以(理论上)始终确定函数内部是否更改了函数参数?

Question 2: In general, can a C compiler (theoretically) always determine whether a function parameter is changed inside the function?

推荐答案

简短答案:否

好答案,不,有证据.

我在用clang编译的MacBook Pro上运行了几次该测试,但没有看到实时差异.

I ran this test, a couple of times, and saw no real time difference, on my MacBook pro compiled with clang:

int add(int a, int b)
{
    return a + b;
}

const int cadd(const int a, const int b)
{
    return a + b;
}

int main (int argc, char * argv[])
{
#define ITERS 1000000000

    clock_t start = clock();
    int j = 0;
    for (int i = 0; i < ITERS; i++)
    {
        j += add(i, i + 1);
    }

    printf("add took %li ticks\n", clock() - start);

    start = clock();
    j = 0;
    for (int i = 0; i < ITERS; i++)
    {
        j += cadd(i, i + 1);
    }

    printf("cadd took %li ticks\n", clock() - start);

    return 0;
}

输出


add took 4875711 ticks
cadd took 4885519 ticks

这些时间确实应该花费一点时间,因为clock并不是最精确的计时功能,并且会受到其他正在运行的程序的影响.

These times really should be taken with a grain of salt, however, as clock isn't the most accurate of timing functions, and can be influenced by other running programs.

因此,这是生成的比较程序集:

So, here is the compared assembly generated:

_add:
    .cfi_startproc
    pushq   %rbp
    .cfi_def_cfa_offset 16
    .cfi_offset %rbp, -16
    movq    %rsp, %rbp
    .cfi_def_cfa_register %rbp
    movl    %edi, -4(%rbp)
    movl    %esi, -8(%rbp)
    movl    -4(%rbp), %esi
    addl    -8(%rbp), %esi
    movl    %esi, %eax
    popq    %rbp
    ret

_cadd:                                 
    .cfi_startproc    
    pushq   %rbp
    .cfi_def_cfa_offset 16
    .cfi_offset %rbp, -16
    movq    %rsp, %rbp
    .cfi_def_cfa_register %rbp
    movl    %edi, -4(%rbp)
    movl    %esi, -8(%rbp)
    movl    -4(%rbp), %esi
    addl    -8(%rbp), %esi
    movl    %esi, %eax
    popq    %rb

因此,如您所见,两者之间没有 没有区别 .将参数传递为const仅是对调用方的提示,该参数将不会更改,并且在上述简单情况下,不会导致编译任何不同的程序集.

So, as you can see, there is No difference between the two. Passing an argument as const is only a hint to the caller the the argument will not be changed, and in a simple scenario like the one described above, will not result in any different assembly compiled.

这篇关于将参数传递给具有const参数的函数:更快吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆