Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

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

by zenmaster (Beadle)
on Mar 26, 2001 at 22:52 UTC ( [id://67290]=note: print w/replies, xml ) Need Help??


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

Who cares which one is faster ?
Larry Wall, Merlyn, And Tom Christiansen who wrote it in the Camel book...
gregw who ask the question, all those who answered...

They don't do the same thing
But they often can achieve the same result.
And usually people use them without even noticing the difference...

But you may be right if you say that we should underline the difference rather than to focus on the speed.
Anyway knowing ALL the aspects of my and local (and speed is one of them) can only be a good thing IMHO.

UPDATE :
This was NOT supposed to be an attack toward Dominus...
But just a rather (goofed) attempt to say : This question (about the speed difference) is interesting even if it's not the most important one (the often ignored difference is more important to me)
The middle of my post, which seems to be the controversial point,
Is not saying my == local ! I just give my idea on why they are usually associated...

I JUST put in bold the important sentence in my post, no other modification was made...

Replies are listed 'Best First'.
(Ovid - my and local are NOT interchangeable)
by Ovid (Cardinal) on Mar 27, 2001 at 12:42 UTC
    zenmaster wrote:
    But they often can achieve the same result.
    And usually people use them without even noticing the difference...
    Well, let's look at a few other things that people use "without even noticing the difference":
    • chop and chomp.

      They're pretty darned close, until you discover the last line of the file you read from doesn't have a newline.

    • &somesub and somesub().

      Usually you won't notice a difference, but the first one makes the sub call and passes the current value of @_ as the arguments. Hmm... if you make such a sub call from within another sub and are expecting no arguments to be passed...

    • $somearray[0] and @somearray[0].

      This one bites quite a few programmers. It usually doesn't cause a problem, but guess what happens when you do something like @vals[3] = somesub() and &somesub uses wantarray? Then you've got trouble and it can be a nasty bug to find.

    • my and local.

      My creates a lexical variable that's private to a package (note that this is NOT a package variable). local temporarily replaces the current package variable with another value.

    Run the following code:

    use strict; use warnings; print test(); sub test { local $x; $x = 17; }
    Guess what? That generates an error similar to the following:
    Global symbol "$x" requires explicit package name at C:\WINNT\Profiles +\Ovid\Desktop\test.pl line 7
    That's because local does not create variables. Replace the local with a my and it works fine.

    For more information, see Coping with Scoping and Seven Useful Uses of local. Both, oddly enough, were written by Dominus, the monk you chose to take to task.

    Cheers,
    Ovid

    Update: After chatting with zenmaster and reading his update, I see that this mostly appears to be a misunderstanding of what he meant to say, but I'll leave the post as it's useful information.

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: is 'my' that much faster than 'local'?
by twerq (Deacon) on Mar 26, 2001 at 23:57 UTC
    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.
      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://67290]
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found