in reply to is 'my' that much faster than 'local'?
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
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: is 'my' that much faster than 'local'?
by gregw (Beadle) on Mar 26, 2001 at 22:22 UTC | |
by kal (Hermit) on Mar 27, 2001 at 14:26 UTC | |
by tilly (Archbishop) on Mar 27, 2001 at 17:23 UTC | |
by gregw (Beadle) on Mar 27, 2001 at 20:33 UTC |