Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: is 'my' that much faster than 'local'?

by twerq (Deacon)
on Mar 26, 2001 at 23:57 UTC ( [id://67310]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: is 'my' that much faster than 'local'?
in thread is 'my' that much faster than 'local'?

But they often can achieve the same result. And usually people use them without even noticing the difference... And ther is a difference. . . a quick example:
use Time::HiRes qw(gettimeofday); $start_time = gettimeofday(); $global_variable = "hello!"; for ($i = 0; $i < 10000000; $i++) { # replace this with "my" local $global_variable = "redefined. . "; } print gettimeofday - $start_time." seconds\n"; print "$global_variable\n";
outputs
31.4016380310059 seconds hello!
and
14.1771370172501 seconds hello!
when using "my" instead.

Replies are listed 'Best First'.
Re: Re: is 'my' that much faster than 'local'?
by sierrathedog04 (Hermit) on Mar 27, 2001 at 03:05 UTC
    Using twerq's original script, I got similar results on an ActiveState Perl/Win32 machine: my was more than twice as fast as local.

    However, I modified twerq's script so that each variable is only declared once. Here is the modified script, which measures time to access or change variables rather than to declare them:

    # replace local with "my" local $start_time = time; local $global_variable = "hello!"; for (local $i = 0; $i < 5000000; $i++) { $global_variable = "redefined. . "; } print time - $start_time." seconds\n"; print "$global_variable\n";
    The program now takes 24 seconds when local is used and 18 when my is used. So it appears that local can introduce significant overhead into a program even when the program only declares variables a few times but accesses them over and over.
      Says sierrathedog04:
      So it appears that local can introduce significant overhead into a program even when the program only declares variables a few times but accesses them over and over.
      Sure. Global variables must be looked up in the symbol table. This is essentially a hash lookup. Lexical variables are retrieved from the scratchpad, which is an array. Arrays are always going to beat hashes for speed.

      But I still think my main point is correct: It doesn't matter which one is faster, because they do different things. You use local when it is appropriate, and my when you need my, not because it is faster.

      Comparing them for speed doesn't make sense. What would you think if someone asked you whether open was faster or slower than getprotoent? You can write all the benchmarks you want to compare open with getprotoent, and none of them are going to mean a thing.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-07-15 06:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.