问题描述
我认为引用传递应该比值传递更快,因为计算机不是在复制数据,它只是指向数据的地址。
I thought that pass by reference should be faster then pass by value because the computer isn't copying data, it just points to the address of data.
但是,请考虑以下C ++代码:
But, consider the following C++ code:
#include <iostream>
#include <cassert>
#include <cmath>
using namespace std;
// do not use pass by reference& with this function, it will become so slow
unsigned long long computePascal(const unsigned int row, const unsigned int position) {
if (position == 1 || position == row)
return 1L;
unsigned long long left = computePascal(row-1, position-1);
unsigned long long right = computePascal(row-1, position);
unsigned long long sum = left + right;
return sum;
}
int main() {
int row, position;
cout << "Input row and position: ";
cin >> row >> position;
assert(position > 0 && position <= row);
unsigned long long pascalNumber = computePascal(row, position);
cout << pascalNumber << endl;
return 0;
}
现在,这段代码只是一个普通程序,通过输入
Now this code is just a normal program that compute pascal number recursively by entering the desired row and position of the triangle.
我尝试将第50行和位置7放置在一起,它的计算时间约为1秒(按值传递)。产量大约是1300万,这是正确的。因此,我认为通过引用代替引用可能会更快,因为它不需要复制大量数据。但这是非常错误的,我不知道为什么要花费更长的时间才能实现价值传递的三倍。
问题是...
I tried putting row 50 and position 7 and it computes around 1 second (pass by value). The output is about 13 million which is correct. So I thought that it could be faster if I pass by reference instead because it doesn't need to copy a lot of data. But this is very wrong and I don't know why it took longer time about 3 times the amount it took for passing by value.The question is ...
为什么在我尝试将其更改为通过const引用传递后,该程序的计算速度如此之慢?
是否重要,因为递归函数是您应按值传递而不是const引用的例外吗?
Does it matter that because recursive function is an exception that you should pass by value rather than const reference?
推荐答案
哪个更快的答案通常是取决于。
The answer to "Which is faster" is usually "It depends".
如果不是传递4个字节的数据,而是传递8个字节的数据指针,那么您真的不能指望这样做会使速度更快。如果不是传递100个字节的数据,而是传递一个8字节的数据指针,那是不同的。
If instead of passing four bytes of data you are passing an eight byte pointer to data, then you can't really expect that to make things faster. If instead of passing 100 bytes of data you are passing an eight byte pointer to data, that's different.
但是现在该函数没有数据,它只有一个引用。因此,无论何时需要读取数据,都必须通过引用间接进行。这需要更长的时间。如果传递一个100字节的对象并且仅读取其中的8个字节,那么您仍然有可能获胜。但是,如果您实际上读取了所有数据,并且可能读取了多次,那么即使对于大型对象,传递值也很容易。
But now the function doesn't have the data, it only has a reference. So whenever it needs to read the data, it has to do that indirectly through the reference. That takes longer. If you pass a 100 byte object and only read eight byte of it, you still are likely to win. But if you actually read all the data, and maybe multiple times, then it could easily be faster to pass the value even for large objects.
真正的区别在于传递对象时,按值传递意味着将调用一个或多或少复杂的构造函数。通过引用传递意味着没有构造函数。但是int仍然没有构造函数。
The real difference comes when you pass an object, and passing by value means a more or less complex constructor will be called. Passing by reference means no constructor. But int has no constructor anyway.
然后就是优化。通过值传递意味着编译器知道您的函数是唯一可以访问数据的函数。通过引用传递意味着数据可以在任何地方。如果您有两个int&参数,我可以两次传递一些int值。因此,增加行可能会增加排名。否则可能不会。这扼杀了优化。
And then there is optimisation. Passing by value means the compiler knows your function is the only one with access to the data. Pass by reference means the data could be anywhere. If you have two int& parameters, I could pass the some int twice. So increasing row might increase pos. Or it might not. That kills optimisations.
然后是优化规则:衡量。您测量了一下,发现更快了。有时候,事情没有任何充分的理由就会变得更快或更慢。
And then there is the rule of optimisation: "Measure it". You measured it and found what's faster. Sometimes things are faster or slower for no good reason whatsoever.
这篇关于哪个更快?按引用传递与按值传递C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!