![]() |
Frequently accessing structure member VS. create a copy variable
Hello,
Let's say a function receives a structure that has 5 member variables. Each of the variables are used many times in this function. Would it be more efficient to access the members time and time again (using the dot operator), or would it be better to access each of them for one time, by copying the value to 5 new variables in that function, which will then be used instead? Creating new copy variables would make the code more readable as it is shorter without so many dot operations, but if performance is the objective, what would be the normal practice in the industry? Thanks in advance. |
Re: Frequently accessing structure member VS. create a copy variable
I presume you mean passing a reference to the struct. In that case, accessing the struct will require more addressing operations than accessing a copy of the member. For very many operations, this would outweigh the code necessary copy to the internal variables and then copy back.
If you mean passing the actual struct, copied struct members will be as readily accessible as a local variable copy, because the struct is essentially a local variable. This causes the additional problem of getting the modified local struct back into the 'real' struct. To see this for yourself, you might want to examine the emitted code. |
Re: Frequently accessing structure member VS. create a copy variable
>if performance is the objective, what would be the normal practice in the industry?
The normal practice is not to optimize unless you have damn good reason to. That means measuring everything under the sun, then if the performance hit is so significant that you can warrant the development time of an optimization, go ahead and do it. Most of the time you won't end up optimizing because it's not worth it. |
Re: Frequently accessing structure member VS. create a copy variable
There's one way I can think of in which, if the struct is a local copy, and not a reference, you might still benefit from keeping an extra copy of some variables. Suppose your struct has some functions that modify only some of the parameters. For example, vectors let you set elements, and that doesn't change the return value of size(). Unless the functions are inlinable, your compiler will have no way of knowing if the members have been modified, and maybe you'll have to access memory (or the processor cache) instead of a register. Which might cost you time. Might not.
I don't see why you would be asking this question, though. It's not usually going to make a significant difference in performance in any way that's worth sacrificing readability, and if it were an important difference, you'd be using experiments, not speculation, to maximize your performance. |
Re: Frequently accessing structure member VS. create a copy variable
I don't consider it much of a readability issue, anyway, unless you name your local reference to the struct something atrociously long. MyStruct.varA make it obvious that the struct is being modified and reduces the chance of an error in moving the stuff into and out of temporary variables. The cost is a couple of instructions per modification.
You do want to avoid pre-optimization unless you're in a resource-tight situation, such as might be the case with an embedded application. Then you just write the tightest code you know how to write and optimize where necessary if the job doesn't get done. |
Re: Frequently accessing structure member VS. create a copy variable
Quote:
Likewise, explicitly doing this sort of thing for clarity is a double-edged sword. Depending on the situation involved, it might actually make your code harder to follow. |
Re: Frequently accessing structure member VS. create a copy variable
>Depending on the situation involved, it might actually make your code harder to follow.
It might make it slower as well. Imagine how a processor will try to predict branches for you and usually be right if your machine code is written well, but if it's not written well there's a serious penalty for the failed prediction. Compilers use a heuristics-based approach toward optimization, and if your code is unconventional enough to sidestep those heuristics, you lose the optimization. Compiler writers go to a lot of trouble to optimize your code for you; you should take advantage of that. :) |
| All times are GMT -5. The time now is 3:16 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC