From: "Steve Graegert" <graegerts@gmail.com>
To: Glynn Clements <glynn@gclements.plus.com>
Cc: Shriramana Sharma <jamadagni@gmail.com>,
linux-c-programming@vger.kernel.org
Subject: Re: efficiency in passing a value to a function
Date: Thu, 5 Apr 2007 15:02:07 +0200 [thread overview]
Message-ID: <6a00c8d50704050602j1c7a7543le5ebeb3b19994977@mail.gmail.com> (raw)
In-Reply-To: <17940.58896.512698.269693@cerise.gclements.plus.com>
On 4/5/07, Glynn Clements <glynn@gclements.plus.com> wrote:
>
> Steve Graegert wrote:
>
> > > I think passing as const & would be more efficient since passing by
> > > value would involve copying the value whereas passing by const & would
> > > skip this step. Am I right? Or is there something else?
> >
> > I have seen many programs making use const reference parameters in
> > order to inform the compiler that the parameter is read-only, and
> > hence should be better optimized.
> >
> > Unfortunately, this intent is at odds with the C++ language
> > definition. The const keyword says that the storage may not be
> > modified through the given name. What it does not say is that the
> > storage cannot be modified through some other name.
> >
> > With the exception of variables directly declared const, which means
> > you can only initialize them, const is basically ineffective a
> > improving run-time performance. It does, however, catch errors in the
> > programming process.
>
> Using a const qualifier still allows the compiler to optimise the
> caller. E.g. if it computes a complex expression involving a variable,
> passes a pointer/reference to that variable to a function, then
> subsequently uses the result of the expression, it doesn't have to
> re-compute the expression if the pointer/reference has a const
> qualifier.
>
> In any case, I suspect that the OP was talking primarily about passing
> references rather than values, rather than about const qualifiers
> per se.
This might indeed be true.
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.
\Steve
--
Steve Grägert <steve@graegert.com>
Jabber xmpp://graegerts@jabber.org
Internet http://eth0.graegert.com, http://blog.graegert.com
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
next prev parent reply other threads:[~2007-04-05 13:02 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 [this message]
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=6a00c8d50704050602j1c7a7543le5ebeb3b19994977@mail.gmail.com \
--to=graegerts@gmail.com \
--cc=glynn@gclements.plus.com \
--cc=jamadagni@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).