Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Is there any reason not to declare variables inside loops or is this something the optimizer takes care of?

Replies are listed 'Best First'.
Re: Performance question
by cwest (Friar) on Jul 20, 2000 at 18:45 UTC
    Let's let Benchmark do the talking...
    my( $name, $ascii ); foreach ( qw/ONE TWO THREE FOUR/ ) { $name = ucfirst lc; $ascii = $name; $ascii =~ s/(.)/ord $1/ge; print $ascii; last if $ascii =~ /\d{6,}/; } undef $name; undef $ascii; # or foreach ( qw/ONE TWO THREE FOUR/ ) { my $name = ucfirst lc; my $ascii = $name; $ascii =~ s/(.)/ord $1/ge; print $ascii; last if $ascii =~ /\d{6,}/; }
    I decided to do this with a couple of sets of iterations...
    Benchmark: timing 100000 iterations of InLoop, OutLoop... InLoop: 4 wallclock secs ( 3.23 usr + 0.00 sys = 3.23 CPU) @ 30 +959.75/s (n=100000) OutLoop: 3 wallclock secs ( 3.59 usr + 0.00 sys = 3.59 CPU) @ 27 +855.15/s (n=100000) Benchmark: timing 1000000 iterations of InLoop, OutLoop... InLoop: 38 wallclock secs (32.36 usr + 0.00 sys = 32.36 CPU) @ 30 +902.35/s (n=1000000) OutLoop: 40 wallclock secs (34.82 usr + 0.00 sys = 34.82 CPU) @ 28 +719.13/s (n=1000000) Benchmark: timing 10000000 iterations of InLoop, OutLoop... InLoop: 372 wallclock secs (322.27 usr + 0.09 sys = 322.36 CPU) @ + 31021.22/s (n=10000000) OutLoop: 411 wallclock secs (347.93 usr + 0.23 sys = 348.16 CPU) @ + 28722.43/s (n=10000000)
    Looks pretty close to me, IMHO Initialize _inside_ the loop.

    Enjoy!

    --
    Casey
    
RE: Performance question
by mrmick (Curate) on Jul 20, 2000 at 17:21 UTC
    When I refer to a 'loop' here, I am actually generalizing to a 'block'.

    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.

    Mick
Re: Performance question
by btrott (Parson) on Jul 20, 2000 at 19:31 UTC
    If you declare a variable inside a loop that's not affected by the variance of the loop--by a loop variable or whatnot--I believe the optimizer will take the declaration/definition of that variable outside of the loop. I've read this somewhere, but I can't remember where, unfortunately.