On Fri, Sep 08, 2006 at 23:38 -0400, Uri Guttman wrote:
> >>>>> "CD" == Chris Dolan <[EMAIL PROTECTED]> writes:
> 
>   CD> Changing the topic a little bit...  I've always suspected there was a
>   CD> speed difference between local and my.  So, I just benchmarked the
>   CD> two versions, and "my" wins by a wide margin:
[...]
> makes perfect sense. my vars are not in the symbol table and just need
> an initialization at run time and they exist in the pad of a sub. localized
> vars need to save the previous value in some form of a stack (real work)
> and also get initialized. so local should be slower.

And using no variable is again quite a bit faster (A thing I found very
disappointing for the sake of readable code).

| ice:~/some_random_test>./perl_local_my_speed_test.pl
|            Rate  local     my global  novar
| local  549356/s     --   -16%   -22%   -39%
| my     656410/s    19%     --    -7%   -27%
| global 703297/s    28%     7%     --   -21%
| novar  895105/s    63%    36%    27%     --

| ice:~/some_random_test>perl -v
| 
| This is perl, v5.8.8 built for i386-freebsd-64int
| (with 1 registered patch, see perl -V for more detail)

Here is the modified script.

#!/usr/bin/perl -w

use strict;
use Benchmark qw(cmpthese);

cmpthese(1_000_000, {
   'local' => sub {
      flatten_copy_local('bar');
   },
   'my' => sub {
      flatten_copy_my('bar');
   },
   'global' => sub {
      flatten_copy_global('bar');
   },
   'novar' => sub {
      flatten_copy_novar('bar');
   },
});

sub flatten_copy_local {
   local $_ = shift;
   ref $_ eq 'SCALAR' ? "$$_" : "$_";
}

sub flatten_copy_my {
   my $s = shift;
   ref $s eq 'SCALAR' ? "$$s" : "$s";
}

sub flatten_copy_novar {
   ref $_[0] eq 'SCALAR' ? "${$_[0]}" : "$_[0]";
}

no strict 'vars';
sub flatten_copy_global {
   $s = shift;
   ref $s eq 'SCALAR' ? "$$s" : "$s";
}

==============

CU,
    Sec
-- 
The Feynman problem solving Algorithm
                        1) Write down the problem
                        2) Think real hard
                        3) Write down the answer

Reply via email to