Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Two Questions on "my"

by bobn (Chaplain)
on May 22, 2004 at 18:10 UTC ( [id://355601]=note: print w/replies, xml ) Need Help??


in reply to Two Questions on "my"

I don't know where you get a 50% hit for the 'my' inside the for loop. When I do this:
$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]; }
I get:
[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
Changing the subs to:
sub getValue { return } sub codeThatUsesFoo { return }
yields:
[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
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.

--Bob Niederman, http://bob-n.com

All code given here is UNTESTED unless otherwise stated.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-16 23:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found