linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Shriramana Sharma <samjnaa@gmail.com>
To: linux-c-programming@vger.kernel.org
Subject: Re: efficiency in passing a value to a function
Date: Sun, 08 Apr 2007 20:47:13 +0530	[thread overview]
Message-ID: <46190779.3040508@gmail.com> (raw)
In-Reply-To: <6a00c8d50704050602j1c7a7543le5ebeb3b19994977@mail.gmail.com>

Steve Graegert wrote:

> If a programmer has chosen to pass a reference rather than a value,
> even if no reference is actually required, intentions to improve
> run-time performance come into mind.  It is much more intuitive to
> pass primitives by value and use references only where appropriate,
> since performance gains due to the use of the const qualifier are very
> small and negligible in most cases.

I am somewhat confused. As I see it, the usage of a const ref for 
passing a parameter by reference is to improve runtime performance, yes, 
but it is not through the "const" part of it. It is through the 
reference part of it, which avoids a copy of the parameter being 
constructed which can be costly with large classes or structs. The const 
part of it is only there to prevent the function from modifying the 
source data, whether intentionally or accidentally. There may be a 
side-effect of optimization, but at least to my understanding the intent 
of const here is making the source read-only. Is this understanding wrong?

I agree of course that in case of tiny tidbits of parameters like a few 
int-s, double-s or even std::string-s, there is not much meaning in 
using const ref-s, and it is more intuitive to pass these by value. It 
is only practically useful to pass big classes / struct-s by reference.

Shriramana Sharma.


  reply	other threads:[~2007-04-08 15:17 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
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 [this message]
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=46190779.3040508@gmail.com \
    --to=samjnaa@gmail.com \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).