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>.
Make sure you measure the right thing:
#include <iostream>
#include <vector>
size_t f1(std::vector<int> v)
{
return v.size();
}
size_t f2(const std::vector<int>& v)
{
return v.size();
}
size_t f3(std::vector<int>* v)
{
return v->size();
}
int main()
{
std::vector<int> v(1000000); // empty vector
std::cout << "Start" << std::endl;
for (int i = 0; i < 1000; ++i)
f1(v);
std::cout << "f1 done" << std::endl;
for (int i = 0; i < 1000; ++i)
f2(v);
std::cout << "f2 done" << std::endl;
for (int i = 0; i < 1000; ++i)
f3(&v);
std::cout << "f3 done" << std::endl;
return 0;
}
This shows the overhead of passing a vector with 10^6 elements by value
instead of by pointer/reference. If you want to be sure you can
rearrange the calls so that f1() is called in second or third and see
what takes the longest.
--
Erik Wikstr=F6m- Hide quoted text -
- Show quoted text -
I ran your program and I do see the big difference. I really don't
know why my benchmark doesn't work.
I should review my code. Erik, Thomas, Ali, Juha, Soumen and Ron thank
you.