S
Saeed Amrollahi
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:
#include <vector>
#include <iostream>
using namespace std;
void f1(std::vector<int> v)
{
cout << "f1 called" << '\n';
for (std::vector<int>::size_type sz = 0; sz < v.size(); sz++)
v[sz] = sz;
cout << "f1 end" << '\n';
}
void f2(std::vector<int> v)
{
cout << "f2 called" << '\n';
for (std::vector<int>::size_type sz = 0; sz < v.size(); sz++)
v[sz] = sz;
cout << "f2 end" << '\n';
}
void f3(std::vector<int>* pv)
{
cout << "f3 called" << '\n';
for (std::vector<int>::size_type sz = 0; sz < pv->size(); sz++)
(*pv)[sz] = sz;
cout << "f3 end" << '\n';
}
int main()
{
vector<int> v(50000000);
f1(v);
f2(v);
f3(&v);
return 0;
}
I ran under Visual Studio 2005, Visual Studio 2008 and GCC. Is it
about some compiler switches that I should on/off? Did compilers
become more clever?
In advance, thank you for your comments.
- Saeed Amrollahi
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:
#include <vector>
#include <iostream>
using namespace std;
void f1(std::vector<int> v)
{
cout << "f1 called" << '\n';
for (std::vector<int>::size_type sz = 0; sz < v.size(); sz++)
v[sz] = sz;
cout << "f1 end" << '\n';
}
void f2(std::vector<int> v)
{
cout << "f2 called" << '\n';
for (std::vector<int>::size_type sz = 0; sz < v.size(); sz++)
v[sz] = sz;
cout << "f2 end" << '\n';
}
void f3(std::vector<int>* pv)
{
cout << "f3 called" << '\n';
for (std::vector<int>::size_type sz = 0; sz < pv->size(); sz++)
(*pv)[sz] = sz;
cout << "f3 end" << '\n';
}
int main()
{
vector<int> v(50000000);
f1(v);
f2(v);
f3(&v);
return 0;
}
I ran under Visual Studio 2005, Visual Studio 2008 and GCC. Is it
about some compiler switches that I should on/off? Did compilers
become more clever?
In advance, thank you for your comments.
- Saeed Amrollahi