简体   繁体   English

为什么const int比const int更快?

[英]Why is const int faster than const int&?

I noticed this accidentally one day, and now decided to test it extensively. 有一天我意外地注意到了这一点,现在决定对它进行广泛的测试。

So, when I call a function: 所以,当我调用一个函数时:

#define Type int
#define Prm const Type &
Type testfunc1(Prm v1, Prm v2, Prm v3, Prm v4, Prm v5, Prm v6, Prm v7, Prm v8, Prm v9, Prm v10){
    return (v1|v2|v3|v4|v5|v6|v7|v8|v9|v10);
}

100 million times: 1亿次:

        for(Type y = 0; y < 10000; y++){
            for(Type x = 0; x < 10000; x++){
                out |= testfunc1(x,y,x,x,y,y,x,y,x,y);
            }
        }

With types int , const int and const int & , i notice that const int is faster than const int & . 使用intconst intconst int & ,我注意到const intconst int &快。 (Note: im using the return value to ensure the function wont get optimized off). (注意:我使用返回值来确保函数不会被优化掉)。

Why is it so? 为什么会这样? I always thought adding & would actually make it faster, but the tests say the opposite. 我一直以为添加&实际上会让它变得更快,但测试却反过来说。 I know for bigger datatypes it would probably be different outcome, I didnt test those though since I'm quite sure about the results. 我知道对于更大的数据类型,它可能会有不同的结果,我没有测试那些,因为我非常肯定结果。

My tests: 我的测试:

const int: 7.95s
const int &: 10.2s

Edit: I think it is indeed because of my architecture; 编辑:我认为这确实是因为我的架构; I tested with Sint64 type and the results were: 我测试了Sint64类型,结果如下:

const Sint64: 17.5s
const Sint64 &: 16.2s

Edit2: Or is it? 编辑2:或者是吗? Tested with double type (which is 64bit?), and results make me puzzled: 测试了double类型(64位?),结果让我感到困惑:

const double: 11.28s
const double &: 12.34s

Edit3: updated the loop code to match my newest tests with 64bit types. Edit3:更新了循环代码,使我的最新测试与64位类型相匹配。

By putting a & into the argument, you are adding more code to the program. 通过在参数中加入& ,您将向程序添加更多代码。 Without the & , the sequence is: 没有& ,顺序是:

 push values
 call Function
 pop values <- usually an update to stack pointer

and in Function: 和功能:

 return sp[arg1] | sp[arg2] | etc <- value read direct from stack.

Adding the '&' does this: 添加'&'可以做到这一点:

 push address of value1
 push address of value2
 etc
 call Function
 pop values <- usually an update to stack pointer

and in Function: 和功能:

 return_value = 0;
 address = sp[arg1]
 or return_value, [address]
 address = sp[arg2]
 or return_value, [address]
 etc
 return return_value

So, as you can see, the & adds a lot. 所以,正如你所看到的, &增加了许多。 So why use it? 那么为什么要用呢? If you have a very large object, passing a pointer is more optimal than copying the object to the stack. 如果您有一个非常大的对象,则传递指针比将对象复制到堆栈更为理想。

This result is heavily system-dependent. 该结果严重依赖于系统。 It indicates that on your particular system copying a value of a reference (which is most likely implemented as a pointer) has higher cost than copying a value of an integer. 它表明在您的特定系统上复制引用的值(最有可能实现为指针)比复制整数值的成本更高。 The most probable reason for that difference is that your integer requires 32-bits to represent, and your pointer/reference representation requires 64-bits. 这种差异的最可能原因是您的整数需要32位来表示,而您的指针/引用表示需要64位。 EDIT This is not to mention the cost of accessing your integers: getting their values would require an additional indirection. 编辑这并不是说访问整数的成本:获取它们的值需要额外的间接。 Since you are passing only two items, use of caching hides that additional cost to a large extent, but the cost is there. 由于您只传递了两个项目,因此使用缓存会在很大程度上隐藏额外成本,但成本却存在。

You are absolutely right about larger types, though: passing a reference to, say, a large struct or a vector<...> still requires only 64 bit (or whatever that size is on your system), regardless of how many items your structure has, or how many items your vector<...> holds. 但是对于较大的类型你是绝对正确的:传递一个引用,比如一个大的struct或一个vector<...>仍然只需要64位(或者你的系统上的任何大小),无论你有多少项目结构有,或者你的vector<...>拥有多少项。 The larger the structure, the higher the costs to pass it by value, and therefore the savings that you realize by making it a reference. 结构越大,通过价值传递的成本就越高,因此通过将其作为参考来实现节省。

Passing addresses instead of values causes the addresses to escape (look up escape analysis or points-to analysis in your favorite compiler textbook), making optimization more difficult. 传递地址而不是值会导致地址转义(在您喜欢的编译器教科书中查找转义分析或指向分析),使得优化更加困难。

Yes, things like inlining and link-time optimization can mitigate these issues. 是的,内联和链接时优化等内容可以缓解这些问题。

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

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