+2  A: 

I would guess most compilers produce the same code for both cases and an int, but I personally prefer the second option since i is in the smallest possible scope.

honk
+3  A: 

If i is a POD type (like the int shown in your example), there will almost certainly be no difference at all.

If i is of some type that has a nontrivial constructor or an assignment operator that does something exciting, then there might be a huge difference and you'd have to compare what the appropriate constructors and assignment operators do. If both blocks are entered, then you'll need to consider the destructor as well.

In general, you shouldn't worry about it. Take the cleaner approach and declare the variable in the most restricted scope possible, as close to its first use as possible, and only refactor it out if your profiler tells you it is a performance problem spot.

James McNellis
nontrivial constructor *or destructor*...?
Tony
It depends which block are entered, yes. If both blocks are entered, you'd get an extra object destruction if you declare the variable as local as possible. If no blocks are entered, obviously you'd get no destructions (and no creations!)
James McNellis
+1  A: 

It's basically the same if it's not a struct/class (though the register allocation is more obviously unconstrained for the intra-block usage which may help the optimiser, but any optimiser that needs that level of help ought to be put into a nursing home).

For classes, it depends on the relative performance of the constructor/destructor and operator=(). Therefore, there's no single, correct answer. Still, creating the variables inside a more localised scope is better programming practice in general, and this concern would usually dominate.

Tony
In that case I'll err on the side of good programming practice rather than trying to eke out tiny optimisations, unless the optimisations turn out in specific cases not to be so tiny.
Innominate