Re: Call by value vs. Call by reference
On Jul 11, 9:54 pm, "Thomas J. Gritzan" <phygon_antis...@gmx.de>
wrote:
Saeed Amrollahi schrieb:> Dear All
Hi
I have learned when an object is big, I should pass the object using a
reference or pointer to it rather than calling by value.
In the following code, I don't gain cosiderable performance with call-
by-reference vs. call-by-value. Indeed, the perforamnce is absolutely
unconsiderable:
[...]
void f1(std::vector<int> v)
[...]
void f2(std::vector<int> v)
[...]
void f3(std::vector<int>* pv)
[...]
There is no reference. You have two function that do a call-by-value and
one that does a call-by-reference (via pointer).
Obviously, the first two shouldn't show any measurable performance
difference.
--
Thomas
Hi
Sorry for typo and confusion. I actually used reference in the code:
void f1(std::vector<int> v)
void f2(std::vector<int>& v)
void f3(std::vector<int>* pv)
Based on guys' advice, I changed my simple benckmark code to better
one:
template<class T>
void f1(std::vector<T> v)
{
for (int sz = 0; sz < 1000000; sz++)
v.push_back(T());
}
template<class T>
void f2(std::vector<T>& v)
{
for (int sz = 0; sz < 1000000; sz++)
v.push_back(T());
}
template<class T>
void f3(std::vector<T>* pv)
{
for (int sz = 0; sz < 1000000; sz++)
pv->push_back(T());
}
int main()
{
vector<int> v; // empty vector
cout << "f1 called" << '\n';
for (int i = 0; i < 10; i++) {
vector<int> v;
f1(v);
}
cout << "f1 end" << '\n';
v.clear();
cout << "f2 called" << '\n';
for (int i = 0; i < 10; i++) {
vector<int> v;
f2(v);
}
cout << "f2 end" << '\n';
v.clear();
cout << "f3 called" << '\n';
for (int i = 0; i < 10; i++) {
vector<int> v;
f3(&v);
}
cout << "f3 end" << '\n';
v.clear();
return 0;
}
I don't use a precise clock, but I expected a difference order of
magnitude efficiency but it is not.
I tested with vector<int>, vector<double> and vector<string>.
Regards,
Saeed