Passing arguments to functions with const parameters: is it faster?

前端 未结 4 687
盖世英雄少女心
盖世英雄少女心 2021-02-07 00:32

Consider, for example:

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

vs.

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


        
4条回答
  •  闹比i
    闹比i (楼主)
    2021-02-07 01:07

    No. both of them should be same speed. for your reason, assume it passes the original values in to sum function, how about some code out of the sum function modify the original value, for example, another thread.

    In general, the const has no impact the performance at arguments. it do impact the performance if the const is a local/global variable because some calculation can be moved to compiling time as if it is a const.

提交回复
热议问题