473,850 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

passing parameters by const& ... can it be overdone?

I have heard it is considered good practice to pass function parameters as
const& as often as possible, is this true? Is it possible to go overboard?
And if so why? Thanks a lot in advance everyone!

Christopher Diggins
http://www.cdiggins.com
Jul 22 '05
20 2152
Peteris Krumins wrote:
E. Robert Tisdale wrote:
That's a good start.

First,
convince us that your compiler did *not* simply inline
f(const DISK_REGISTER&) and/or g(DISK_REGISTER ).
Show us the compiler version and options that you used.
pkrumins$ g++ test.cpp -o test -W -Wall
pkrumins$ g++ -v
gcc version 3.3.4

Since I am [not] optimizing, no functions were inlined.
Second,
convince us that your results are *significant*.
Run each case several times
and calculate the average time and standard deviation.
The difference in average times must be at least as large
as the standard deviation(s).

I changed the number of iterations for each test to 200 million, so
the tests took less time.
I did 10 observations.

Here are the results (sorted by time):

1. f(const DISK_REGISTER&)

N time (s) delta time (s) (delta time)^2 (s^2)
1 6,324 0,0195 0,00038025
2 6,326 0,0175 0,00030625
3 6,327 0,0165 0,00027225
4 6,334 0,0095 0,00009025
5 6,336 0,0075 0,00005625
6 6,350 0,0065 0,00004225
7 6,353 0,0095 0,00009025
8 6,354 0,0105 0,00011025
9 6,363 0,0195 0,00038025
10 6,368 0,0245 0,00060025
----- ----------
avg: 6,3435 sum: 0,00232850

standard deviation:
sqrt(sum/(n - 1)) = 0,016084844 (s)

2. g(DISK_REGISTER )
N time (s) delta time (s) (delta time)^2 (s^2)
1 5,596 0,0048 0,00002304
2 5,597 0,0038 0,00001444
3 5,599 0,0018 0,00000324
4 5,599 0,0018 0,00000324
5 5,600 0,0008 0,00000064
6 5,600 0,0008 0,00000064
7 5,601 0,0002 0,00000004
8 5,603 0,0022 0,00000484
9 5,604 0,0032 0,00001024
10 5,609 0,0082 0,00006724
----- ----------
avg: 5,601 sum: 0,00012760

standard deviation: 0,003765339 (s)
Now the results are significant and we see that pass by value is


((6.3435 - 5.601)/6.3435)*100% = 11.7%
faster than pass by const reference. cat main.cc #include <iostream>
#include <string>

struct DISK_REGISTER {
unsigned ready:1;
unsigned error_occured:1 ;
unsigned disk_spinning:1 ;
unsigned write_protect:1 ;
unsigned head_loaded:1;
unsigned error_code:8;
unsigned track:9;
unsigned sector:5;
unsigned command:5;
};

void f(const DISK_REGISTER &r) {
(void) r;
}

void g(DISK_REGISTER r) {
(void) r;
}

int
main(int argc, char* argv[]) {
int result = EXIT_SUCCESS;
if (1 < argc) {
const std::string arg = argv[1];
DISK_REGISTER r;
if ("g" == arg)
for (unsigned int i = 0; i < 1000000000; ++i)
g(r);
else
if ("f" == arg)
for (unsigned i = 0; i < 1000000000; i++)
f(r);
else {
std::cerr << argv[0] << ": <f|g>" << std::endl;
result = EXIT_FAILURE;
}
}
return result;
}
g++ -Wall -ansi -pedantic -o main main.cc
g++ --version g++ (GCC) 3.4.1 time ./main f 16.945u 0.007s 0:16.95 99.9% 0+0k 0+0io 0pf+0w time ./main f 16.907u 0.005s 0:17.44 96.9% 0+0k 0+0io 0pf+0w
+0k 0+0io 0pf+0w time ./main f 16.893u 0.006s 0:16.90 99.9% 0+0k 0+0io 0pf+0w time ./main f 16.912u 0.005s 0:16.92 99.9% 0+0k 0+0io 0pf+0w time ./main g 16.924u 0.003s 0:16.93 99.9% 0+0k 0+0io 0pf+0w time ./main g 16.921u 0.005s 0:16.92 100.0% 0+0k 0+0io 0pf+0w time ./main g 17.035u 0.006s 0:17.06 99.8% 0+0k 0+0io 0pf+0w time ./main g 17.041u 0.005s 0:17.05 99.9% 0+0k 0+0io 0pf+0w

My compiler emits exactly the same code
for both f(const DISK_REGISTER&) and g(DISK_REGISTER ).
The only difference in the calling program
in the arguments that get puhed onto the stack:
diff --side-by-side --width=80 ff.s gg.s

Jul 22 '05 #21

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

Similar topics

25
2958
by: Victor Bazarov | last post by:
In the project I'm maintaining I've seen two distinct techniques used for returning an object from a function. One is AType function(AType const& arg) { AType retval(arg); // or default construction and then.. // some other processing and/or changing 'retval' return retval; }
0
9741
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11011
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10724
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
10351
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
7899
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
7071
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
5929
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4546
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
2
4140
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.