I get:$a_huge_value = 3200000; my($foo); $time1 = time(); for ($i = 0; $i < $a_huge_value; $i++) { $foo = getValue(); codeThatUsesFoo($foo); } $time2 = time(); for ($i = 0; $i < $a_huge_value; $i++) { my $foo = getValue(); codeThatUsesFoo($foo); } $time3 = time(); print $time2 - $time1, "\n"; print $time3 - $time2, "\n"; sub getValue { return $_[0]; } sub codeThatUsesFoo { return $_[0]; }
Changing the subs to:[bobn@trc2:/home/bobn/misc]# perl my2.pl 14 14 [bobn@trc2:/home/bobn/misc]# perl my2.pl 13 15 [bobn@trc2:/home/bobn/misc]# perl my2.pl 13 15 [bobn@trc2:/home/bobn/misc]# perl my2.pl 14 16
yields:sub getValue { return } sub codeThatUsesFoo { return }
SO the decision is really based on what it *should* be based on - is $foo intended to be used outside the loop, with the effects of the loop desired to be visible. If so, do it the first way. If not, do it the second way.[bobn@trc2:/home/bobn/misc]# perl my2.pl 12 12 [bobn@trc2:/home/bobn/misc]# perl my2.pl 12 13 [bobn@trc2:/home/bobn/misc]# perl my2.pl 13 13 [bobn@trc2:/home/bobn/misc]# perl my2.pl 12 13
--Bob Niederman, http://bob-n.com
All code given here is UNTESTED unless otherwise stated.
In reply to Re: Two Questions on "my"
by bobn
in thread Two Questions on "my"
by C_T
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |