I'm new to c++ programming.
First I have a question. If I "economy" variables, this make program run
faster? or nothing has changed?
it makes difference because in the example2 I used only two variables and in example1 4 variables?
The question is if it have an effect in larger source codes.
This kind of difference isn't something to be concerned about. (Unless the objects are big dynamic allocations and you can save the resources)
Just do whatever you think is best to express the code logically... i.e you shouldn't reuse a variable for something totally different without documenting it.
I wouldn't go out of my way to "economize" on variables, but it does seem to be a tendency for beginners to use too many.
Your second example won't run any faster than the first, but it will use slightly less (stack) space.
Do not multiply objects without necessity. - W. Occam
Premature optimization is the source of much evil™.
Basically, don't worry about such things unless you can prove your program is slow and you can prove that specific code is the culprit.
Readability over optimization. Remember that.
so economising variables save only space , not make program to run faster
If even that. The compiler can optimize away variables entire by storing values directly in the registers.
True. So the fewer variables you use, the more likely they will all be optimized into registers.
Originally Posted by Elysia
Premature optimization is the root of all evil. - D.Knuth
On the other hand, we cannot ignore efficiency. - Jon Bentley