Clear questions and runnable code get the best and fastest answer |
|
PerlMonks |
Re: is 'my' that much faster than 'local'?by arturo (Vicar) |
on Mar 26, 2001 at 21:32 UTC ( [id://67253]=note: print w/replies, xml ) | Need Help?? |
local saves away the value of a global variable and substitutes a new one within the block in which it is called as well as for any code called from that block; my declares a new variable that is only visible within the block in which it is declared. my is faster because it doesn't have to save anything away. My understanding is that my is about 10% faster. Given that variable declaration is probably a small part of the overall program, you will see a whiz-bang speed improvement only if you're calling local a lot (like in loops). The 'gotchas' that lurk have to do with the fact that local acts on global variables, whereas my does not; so if your subroutines call other subroutines that read globals, you could very well get unexpected behavior. However, it's a very good idea to use my when that's what you mean; when you're passing info to your subroutines via globals, you're just asking for confusion. So I would recommend getting it to work under my for reasons other than pure performance. For the longer version of all this, see Variable Scoping in Perl: the basics =) Philosophy can be made out of anything. Or less -- Jerry A. Fodor
In Section
Seekers of Perl Wisdom
|
|