Re: efficiency in passing a value to a function

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Shriramana Sharma 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?

The const is primarily for correctness (ensuring that the callee
doesn't modify the data), but there may also be a performance gain.

-- 
Glynn Clements <glynn@xxxxxxxxxxxxxxxxxx>
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Assembler]     [Git]     [Kernel List]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [C Programming]     [Yosemite Campsites]     [Yosemite News]     [GCC Help]

  Powered by Linux