Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

RE (tilly) 2: redeclaring variables with 'my'

by tilly (Archbishop)
on Nov 11, 2000 at 02:30 UTC ( [id://41040]=note: print w/replies, xml ) Need Help??


in reply to Re: redeclaring variables with 'my'
in thread redeclaring variables with 'my'

There is some run-time action upon entering and exiting its scope, so there is indeed some overhead. It does not need to allocate and deallocate space, but Perl does need to manipulate reference counts and check that the old space allocated is for a variable not in use and so can be reassigned. (Just in case you saved a reference and kept a closure!) But even within this minimal loop the overhead is minor.

So yes, there is a small amount of overhead associated with the safer mechanism. However when you try conciously switching styles between projects you will quickly find that the safer style results in big wins in terms of reliability, development, and debugging. Besides which, aggressively modularizing your code etc gives you opportunities to later find and fix large algorithm mistakes. (Leading to better performance wins than optimizing the heck out of your code up front can do.)

The tiny performance difference is a really, really bad excuse for keeping a bad habit. For more on this topic, there is a sample section from Code Complete online.

  • Comment on RE (tilly) 2: redeclaring variables with 'my'

Replies are listed 'Best First'.
RE: RE (tilly) 2: redeclaring variables with 'my'
by Fastolfe (Vicar) on Nov 11, 2000 at 02:38 UTC
    I agree completely, and have been reluctant to move away from the while (my $var ...) style for that reason (except in cases where I'm expecting to iterate very tightly many thousands of times). A 15% win isn't much when we're talking about, like, picoseconds here. Heh.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://41040]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2024-04-23 17:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found