Re: A question about efficiency

Tech-Archive recommends: Fix windows errors by optimizing your registry



Regarding efficiency, if you pass a parameter by reference because you only
want to avoid the copy, it's better to have a parameter which is a const
reference.

void func2(const float &f, float &g)
{
g = f + 10;
}

Doing this, the call can be made both on a const and non-const f object,
even a literal value.
As a side effect, it allows implicit conversion, for instance from int

In func2, g is then documented as an in/out parameter.

As Uli has suggested for his function add_10, func1 could be write as :
float func1(float f)
{
return (f+10.0f);
}
for an object more complex than a float the compiler may optimize the code
with the "return value optimization" which avoids the creation of temporary
objects.

Fred




.



Relevant Pages

  • Re: A question about efficiency
    ... it doesn't make sense to pass 4-byte const POD (float) by ... if you pass a parameter by reference because you ... > only want to avoid the copy, it's better to have a parameter which is a ...
    (microsoft.public.vc.language)
  • Re: Anders Hejlsberg comment on immutable objects
    ... >explicit interface implementation exists is so an interface name can class ... Improper implicit conversions constitute a nasty ... >> With value types, the variable's value is the object, not a reference ... Consider the affects of our different views on a const ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Another C# critique (major rewrite!)
    ... it would be a different kind of const. ... > pointer, and the vtable to point to the Type. ... needs answering most specifically would be is a reference a literal pointer ... >>Currently I'm thinkg about security and control. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: [PATCH 1/1] pagemap clear_refs: modify to specify anon or mapped vma clearing
    ... values a no-op similar to drop_caches at this point to avoid the potential ... adds flexibility to the user user space application implementing the reference ... That format could really be improved here, ... I am not sure what you mean by "clean" proc.txt, ...
    (Linux-Kernel)
  • Re: [PATCH 1/1] pagemap clear_refs: modify to specify anon or mapped vma clearing
    ... values a no-op similar to drop_caches at this point to avoid the potential ... adds flexibility to the user user space application implementing the reference ... That format could really be improved here, ... I am not sure what you mean by "clean" proc.txt, ...
    (Linux-Kernel)