in reply to Performance question
As far as I know, no reason, unless you want to ensure they be available outside of the loops. Sometimes, if I'm lazy, I will declare a variable outside of said loop just to be sure that it is available someplace else. More often than not, I will assign the value of the variable inside the loop to one that exists outside.
If using a variable whose scope is only for a particular loop, I use my to ensure that it doesn't exist once the loop is done and to make sure that it doesn't conflict with any variables I might have of the same name. -- Like I said - I'm Lazy. --
As far as performance, if the loop iterates many times (I'll let you determine the number here) it would probably be better to declare the variable outside the loop. This way, you won't have to worry about the space being allocated and deallocated each time the loop iterates. This would be a case where by saving the overhead, we can improve performance.
Remember: These are only my opinions and someone may prove me wrong.
|
|---|