Do not guess where your program spends time. First, profile to find bottlenecks, and then optimize them.
As for GetContext() , it depends on how complex it is. If it simply returns a member variable of the class, then it is likely that the compiler will include it. If GetContext() needs to perform a more complex operation (for example, searching for a context in a table), the compiler probably does not insert it, and you can simply call it once, as in the second fragment.
If you use GCC, you can also mark the GetContext() function GetContext() pure attribute. This will allow him to perform more optimizations, such as eliminating a common subexpression .
source share