c++ - Faster to reuse a variable defined in a common scope or redefine it in each inner scope? -
c++ specifically, if matters, imagine answer lies in assembly code somehow.
if have multiple blocks in common scope (say, function) each use variable of same type, faster define variable in common scope , reinitialise in each block, or redefine , initialise in each of blocks (or there no fundamental difference)?
example:
int i; {//block 1 = somefunction(); ... } {//block 2 = someotherfunction(); ... }
versus
{//block 1 int = somefunction(); ... } {//block 2 int = someotherfunction(); ... }
if i
pod type (like int
shown in example), there no difference @ all.
if i
of type has nontrivial constructor or assignment operator exciting, there might huge difference , you'd have compare appropriate constructors , assignment operators do. if both blocks entered, you'll need consider destructor well.
in general, shouldn't worry it. take cleaner approach , declare variable in restricted scope possible, close first use possible, , refactor out if profiler tells performance problem spot.
Comments
Post a Comment