0

Is it worth keeping local variable of an objects member variable if I am going to use that variable multiple times? This would reduce the number of Getter calls, though I feel that might not be necessary.

Ex.

void Foo(Bar b){
  if(b.Dodos() == "skimpy")
    cout << "These Dodos are " << b.Dodos() << endl;
}

Assuming the code is complicated and b.Dodos() is called multiple times, does it make much of a difference to keep a local variable? i.e string bd = b.Dodos()? Especially if one can reasonably assume/know that the getter is just returning a member variable?

(Assume this has asked many times, but I can't find answer)

xcorat
  • 1,434
  • 2
  • 17
  • 34
  • 1
    Try it and see . – M.M May 23 '17 at 04:10
  • Hmmm.. so there's no ~mostly correct standard answer than try it and see? – xcorat May 23 '17 at 04:12
  • The standard has nothing to say about performance (other than algorithmic complexity requirements of containers) – M.M May 23 '17 at 04:13
  • 1
    If it is a inlined function just returning a variable, the compiler is likely going to just make the whole function call disappear, the caching is unlikely to matter. But yes, the simplest way to make sure is to just try it and see – Passer By May 23 '17 at 07:58
  • The biggest performance issue here might be the `==" skimpy"` part. If that's implemented as `std::string::string("skimpy", strlen("skimpy"))` you might have a non-trivial overhead. Compare `"skimpy"s` which is a compile-time std::string literal. – MSalters May 23 '17 at 08:47

1 Answers1

1

If the member function is just returning a member variable, then you likely won't get any benefit from assigning the result to a local variable, especially if you use inlining. If the member function does a lot of work, then keeping a local copy of it can give you a huge performance improvement. However, this only works correctly if the function is pure. That is, if it always returns the same value when called with the same arguments. If the function is both pure and expensive and is called in more than one other function, you might also consider trying the memoization technique. This ensures that if the function is ever called with the same parameters, subsequent calls will involve just a lookup instead of the expensive calculation.

Besides efficiency, you should consider which strategy best follows the DRY principle, or if assigning to a variable will help reveal your intentions to the next person who reads the code (which may be you in the future). For example, if you are calling the function with the same arguments multiple times, you should ask yourself if it's a coincidence that the arguments were the same. If there was a change required, would that change affect all the calls in the same way, or could they affect the calls differently? If the calls would all change together, then assigning to a variable will help with maintenance, but if they would change independently, then the variable would make maintenance more difficult.

David Nehme
  • 21,379
  • 8
  • 78
  • 117