473,786 Members | 2,566 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Call by value vs. Call by reference

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< intv)
{
cout << "f1 called" << '\n';
for (std::vector<in t>::size_type sz = 0; sz < v.size(); sz++)
v[sz] = sz;
cout << "f1 end" << '\n';
}

void f2(std::vector< intv)
{
cout << "f2 called" << '\n';
for (std::vector<in t>::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<in t>::size_type sz = 0; sz < pv->size(); sz++)
(*pv)[sz] = sz;
cout << "f3 end" << '\n';
}
int main()
{
vector<intv(500 00000);
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
Jul 11 '08 #1
7 3523
Hi,
I would expect you see a large difference since if you pass by value it has
to copy the whole vecor, however..

In your code the compiler might have removed the whole vector copying during
optimization since you don't do anything with it. You wouldn't notice a
difference between a program with the vector passed in and one completely
without and without assignment

Regards, Ron AF Greve

http://www.InformationSuperHighway.eu

"Saeed Amrollahi" <s_*********@ya hoo.comwrote in message
news:71******** *************** ***********@w7g 2000hsa.googleg roups.com...
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< intv)
{
cout << "f1 called" << '\n';
for (std::vector<in t>::size_type sz = 0; sz < v.size(); sz++)
v[sz] = sz;
cout << "f1 end" << '\n';
}

void f2(std::vector< intv)
{
cout << "f2 called" << '\n';
for (std::vector<in t>::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<in t>::size_type sz = 0; sz < pv->size(); sz++)
(*pv)[sz] = sz;
cout << "f3 end" << '\n';
}
int main()
{
vector<intv(500 00000);
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

Jul 11 '08 #2
On Jul 11, 1:37*pm, Saeed Amrollahi <s_amroll...@ya hoo.comwrote:
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< intv)
{
* * * * cout << "f1 called" << '\n';
* * * * for (std::vector<in t>::size_type sz = 0; sz < v.size();sz++)
* * * * * * * * v[sz] = sz;
* * * * cout << "f1 end" << '\n';

}

void f2(std::vector< intv)
{
* * * * cout << "f2 called" << '\n';
* * * * for (std::vector<in t>::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<in t>::size_type sz = 0; sz < pv->size(); sz++)
* * * * * * * * (*pv)[sz] = sz;
* * * * cout << "f3 end" << '\n';}

int main()
{
* * * * vector<intv(500 00000);
* * * * 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
Probably because your vector is empty (may be it's just holding a
pointer and few other int)and you're inserting elements inside the
function.
Try inserting elements before calling the function and call the
function 1000 times or so. Also try vector of string and let it have
all same
elements. Let me know what you find.

Regards,
~ Soumen
Jul 11 '08 #3
Saeed Amrollahi wrote:
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:
How do you measure the speed of the program? Maybe copying that vector
is relatively so fast that you don't notice the difference. Try calling
each function in a loop to see if there's a difference.
Jul 11 '08 #4
On Jul 11, 7:08*am, Soumen <soume...@gmail .comwrote:
On Jul 11, 1:37*pm, Saeed Amrollahi <s_amroll...@ya hoo.comwrote:
void f1(std::vector< intv)
{
* * * * cout << "f1 called" << '\n';
* * * * for (std::vector<in t>::size_type sz = 0; sz < v.size(); sz++)
* * * * * * * * v[sz] = sz;
That is not "inserting" an element, rather assigning to an existing
one.
* * * * cout << "f1 end" << '\n';
}
* * * * vector<intv(500 00000);
That is a vector with 50 million ints, not empty.
Probably because your vector is empty (may be it's just holding a
pointer and few other int)and you're inserting elements inside the
function.
That would be this code:

vector<intv; // empty
/* ... */
v.push_back(sz) ; // insert

Ali
Jul 11 '08 #5
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< intv)
[...]
void f2(std::vector< intv)
[...]
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
Jul 11 '08 #6
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< intv)
[...]
void f2(std::vector< intv)
[...]
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< intv)
void f2(std::vector< int>& v)
void f3(std::vector< int>* pv)

Based on
Jul 11 '08 #7
>
* * * * cout << "f1 called" << '\n';
* * * * for (int i = 0; i < 10; i++) {
* * * * * * * * vector<intv;
* * * * * * * * f1(v);
* * * * }
* * * * cout << "f1 end" << '\n';
* * * * v.clear();
* * * * cout << "f2 called" << '\n';
* * * * for (int i = 0; i < 10; i++) {
* * * * * * * * vector<intv;
* * * * * * * * f2(v);
* * * * }
* * * * cout << "f2 end" << '\n';
* * * * v.clear();
* * * * cout << "f3 called" << '\n';
* * * * for (int i = 0; i < 10; i++) {
* * * * * * * * vector<intv;
* * * * * * * * f3(&v);
* * * * }
The problem with your benchmark is that most of the work is done
within the functions themselves.
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.
That's true, but you must understand why. The reason is that when
calling by value, you make a copy of the object, and copying that
object takes time.
But in your example, you just pass an empty vector, and that's quite
quick to copy.
Furthermore, it's only _starting_ the function that suffers when
calling by value, not the work done _inside_ it.

So, to make your benchmark work better, strip the body of your
functions and call your functions more times.
The code below should show the difference quite clearly (part 1 took
about 3 minutes, and part 2 was under a second on my system).
//speedtest.cpp
#include <vector>

#include <iostream>
using namespace std;
double f1(vector<doubl ein) {
return in[0];
}
double f2(vector<doubl e&in) {
return in[1];
}
int main() {

vector<doubleve c(1000000); //big vector
vec[0]=1.2;
vec[1]=3.4;
double db;
int bigNum = 10000;
cout << "start f1:\n" << endl; //remember to flush...
for (int i=0;i< bigNum ;i++) {
db = f1(vec);
}
cout << "start f2:\n" << endl;
for (int i=0;i< bigNum ;i++) {
db = f2(vec);
}
cout << "end" << endl;

return 0;
}
Jul 15 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
1934
by: Mario Thiel | last post by:
Hello, i am new to JavaScript and right now i am learning Call by Value. i tryed to write a small script but somehow it doesnt work right. i cant put <br> into it, and i can only put the document.write(xyz,x) in the function. <html> <head> <script language="JavaScript">
26
45524
by: Dave Hammond | last post by:
In document "A.html" I have defined a function and within the document body have included an IFRAME element who's source is document "B.html". In document "B.html" I am trying to call the function defined in "A.html", but every attempt results in an "is not a function" error. I have tried to invoke the function using parent.document.funcname(), top.document.funcname(), and various other identifying methods, but all result in the above...
39
6555
by: Randell D. | last post by:
Folks, I'm sure this can be done legally, and not thru tricks of the trade - I hope someone can help. I'm writing a 'tool' (a function) which can be used generically in any of my projects. When it completes, it can call a success, or a failure function. The names of these success, or failure functions will differ, and I'd like to know how I can pass the name of a function to my tool, and how my tool can call the function, using that...
35
10794
by: hasho | last post by:
Why is "call by address" faster than "call by value"?
24
2630
by: ALI-R | last post by:
Hi All, First of all I think this is gonna be one of those threads :-) since I have bunch of questions which make this very controversial:-0) Ok,Let's see: I was reading an article that When you pass a Value-Type to method call ,Boxing and Unboxing would happen,Consider the following snippet: int a=1355; myMethod(a); ......
13
26598
by: mitchellpal | last post by:
i am really having a hard time trying to differentiate the two..........i mean.....anyone got a better idea how each occurs?
8
4269
by: priyasmita_guha | last post by:
C uses call by value for passing of parameters in contrast to C++ which uses call by reference.How is call by value advantageous and how is it implemented?
10
16675
by: ravi | last post by:
Hi, i am a c++ programmer, now i want to learn programming in c also. so can anybody explain me the difference b/w call by reference and call by pointer (with example if possible).
22
4192
by: Nehil | last post by:
Does C follow call by value convention or call by reference? i see that there is nothing like reference in C standard but it is referenced. still, what should be the answer for the above question?
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9961
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7512
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4066
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.