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

Who cares which one is faster? They don't do the same thing, so it's like asking if you should use a screwdriver instead of a drill because it's lighter. That's an irrational reason for using a screwdriver. You use a screwdriver when you want to drive screws, not because it's light.

  • Comment on Re: is 'my' that much faster than 'local'?

Replies are listed 'Best First'.
Re: Re: is 'my' that much faster than 'local'?
by zenmaster (Beadle) on Mar 26, 2001 at 22:52 UTC
    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...
      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.

      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.