From: Adam Dyga <adeon@tlen.pl>
To: linux-c-programming@vger.kernel.org
Subject: Re: efficiency in passing a value to a function
Date: Wed, 11 Apr 2007 23:44:25 +0200 [thread overview]
Message-ID: <200704112344.25098.adeon@tlen.pl> (raw)
In-Reply-To: <17939.60957.687289.154272@cerise.gclements.plus.com>
On Wednesday, 4 April 2007 20:27, Glynn Clements wrote:
> Shriramana Sharma wrote:
> > Is providing an input to a function by constant reference more efficient
> > than passing it by value? In what way.
>
> Passing a reference doesn't copy the value. This can be significant if
> the value is large (i.e. a structure); for values which are no larger
> than a pointer, it is unlikely to make any difference.
For values which are not larger than a pointer, passing by reference can be
even (slightly) slower, because the pointer must be dereferenced in the
function in order to use the value:
void f1(const int v)
{
printf("%d", v);
}
void f2(const int & v)
{
printf("%d", v);
}
Compiled with -O2:
(gdb) disas f1
Dump of assembler code for function _Z2f1i:
0x08048460 <_Z2f1i+0>: push %ebp
0x08048461 <_Z2f1i+1>: mov %esp,%ebp
0x08048463 <_Z2f1i+3>: sub $0x10,%esp
0x08048466 <_Z2f1i+6>: mov 0x8(%ebp),%eax
0x08048469 <_Z2f1i+9>: push %eax
0x0804846a <_Z2f1i+10>: push $0x80485d4
0x0804846f <_Z2f1i+15>: call 0x8048358 <printf@plt>
0x08048474 <_Z2f1i+20>: leave
0x08048475 <_Z2f1i+21>: ret
End of assembler dump.
(gdb) disas f2
Dump of assembler code for function _Z2f2RKi:
0x08048480 <_Z2f2RKi+0>: push %ebp
0x08048481 <_Z2f2RKi+1>: mov %esp,%ebp
0x08048483 <_Z2f2RKi+3>: sub $0x10,%esp
0x08048486 <_Z2f2RKi+6>: mov 0x8(%ebp),%eax
0x08048489 <_Z2f2RKi+9>: mov (%eax),%edx //pointer dereference
0x0804848b <_Z2f2RKi+11>: push %edx
0x0804848c <_Z2f2RKi+12>: push $0x80485d4
0x08048491 <_Z2f2RKi+17>: call 0x8048358 <printf@plt>
0x08048496 <_Z2f2RKi+22>: leave
0x08048497 <_Z2f2RKi+23>: ret
End of assembler dump.
Adam
next prev parent reply other threads:[~2007-04-11 21:44 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-04-04 17:13 efficiency in passing a value to a function Shriramana Sharma
2007-04-04 18:27 ` Glynn Clements
2007-04-11 21:44 ` Adam Dyga [this message]
2007-04-04 18:38 ` Steve Graegert
2007-04-05 12:05 ` Glynn Clements
2007-04-05 13:02 ` Steve Graegert
2007-04-08 15:17 ` Shriramana Sharma
2007-04-13 17:23 ` Glynn Clements
2007-04-14 13:15 ` Adam Dyga
2007-04-08 14:45 ` Shriramana Sharma
2007-04-13 17:51 ` Glynn Clements
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=200704112344.25098.adeon@tlen.pl \
--to=adeon@tlen.pl \
--cc=linux-c-programming@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.