Michael et al,

>$ perl5.8.0 -MDevel::Peek -wle '%h =3D (foo =3D> 42, bar =3D> 23, baz =3D=
>> 27);  print Dump \%h'
>SV =3D RV(0x10226420) at 0x101d2098
>  REFCNT =3D 1
<snip>

Eeek! I think I'll just leave that technique for the "trained
professionals". Thanks any :-) I'll continue to use length as recommended.

>> 1) The code:
>>      next if $seen{$_}; $seen{$_}=3D1;
>> according to my benchmarking is faster than
>>      unless ($seen{$_}++)  {...}
>> even though the former looks up $seen{$_} twice, and ++ is a pretty tri=
>vial
>> operator. Its not where I'd have put my money.
>
>Here's what I get on Linux.
>
>$ perl5.6.1 ~/src/bench/next_vs_unless 200000
>Benchmark: timing 200000 iterations of control, next, unless...
>   control:  1 wallclock secs ( 0.18 usr +  0.00 sys =3D  0.18 CPU) @
111=1111.11/s (n=3D200000)
>            (warning: too few iterations for a reliable count)
>      next:  2 wallclock secs ( 1.74 usr +  0.00 sys =3D  1.74 CPU) @
114=942.53/s (n=3D200000)
>    unless:  1 wallclock secs ( 1.17 usr +  0.02 sys =3D  1.19 CPU) @
168=067.23/s (n=3D200000)
>             Rate    next  unless control
>next     114943/s      --    -32%    -90%
>unless   168067/s     46%      --    -85%
>control 1111111/s    867%    561%      --

The point of the benchmark was to decide which was the better way of
building the %seen hash. Your code isn't really fair as the hash only has
one element. My code makes %seen enormous, which is precisely when I though
a double request of a hash would be inefficient. This was my benchmark
script.

use strict;
use Benchmark;

my (%x,%y);
$y{$_}=$x{$_}=&next_key() for (0..1e6);

sub next_key {
        return (rand() < 0.1) ? "Common".int(rand(1e2)) :
"Rare".int(rand(1e8))
}
timethese(-5, { 
        'control'  => sub {my $k=&next_key()},
    'plusplus' => sub {my $k=&next_key(); $x{$k}++ },
    'assign1'  => sub {my $k=&next_key(); unless ($y{$k}) {$y{$k}=1}}
});

Benchmark: running assign1, control, plusplus, each for at least 5 CPU
seconds...
   assign1:  5 wallclock secs ( 5.09 usr +  0.00 sys =  5.09 CPU) @
39536.66/s (n=201123)
   control:  5 wallclock secs ( 5.14 usr +  0.00 sys =  5.14 CPU) @
50786.60/s (n=260840)
  plusplus:  6 wallclock secs ( 5.03 usr +  0.00 sys =  5.03 CPU) @
36624.03/s (n=184109)


>> 2) There is a bug Activestate Perl 5.6.0 and onwards for Win32 where
garbage
>> collection  of %seen=() or %seen=undef is slow.  I reported the bug back
in
>> December, but nothing seems to have happend since.
>> http://bugs.activestate.com//ActivePerl/show_bug.cgi?id=3D18559
>> I'm surprised there's been no action on this one as garbage collecting
>> hashes I would have thought was critical to object oriented programming.
>
>According to Bugzilla, Guru was unable to reproduce the problem:

Well on Unix and Win32 5.005 this code runs each loop in constant time
(10,10,10,10). In ActiveState Win32 5.6.0 and 5.6.1 only the first one takes
(10,24,23,23). What do you get?

use strict;
srand(); 
for (1..4) { 
        print "Loop $_ took ".&Do_It." seconds\n"; 
} 

sub Do_It { 
        my $start=time; 
        my %x=(); 
        keys(%x)=1e5; 
        for (my $i=0; $i<1e5;$i++) { 
                $x{rand().rand().rand().rand().rand().rand()}=$i; 
        } 
        return time-$start; 
} 


Alistair

> ----------------------------------------------------------------------
> Alistair McGlinchy,           [EMAIL PROTECTED]
> Sizing and Performance, Central IT,   ext. 5012,   ph +44 20 7268-5012
> Marks and Spencer, 3 Longwalk Rd, Stockley Park, Uxbridge UB11 1AW, UK 
> 


-----------------------------------------------------------------------


Registered Office:
Marks & Spencer p.l.c
Michael House, Baker Street,
London, W1U 8EP
Registered No. 214436 in England and Wales.

Telephone (020) 7935 4422 
Facsimile (020) 7487 2670

www.marksandspencer.com

Please note that electronic mail may be monitored.

This e-mail is confidential. If you received it by mistake, please let us know and 
then delete it from your system; you should not copy, disclose, or distribute its 
contents to anyone nor act in reliance on this e-mail, as this is prohibited and may 
be unlawful.

The registered office of Marks and Spencer Financial Services Limited, Marks and 
Spencer Unit Trust Management Limited, Marks and Spencer Life Assurance Limited and 
Marks and Spencer Savings and Investments Limited is Kings Meadow, Chester, CH99 9FB.

Reply via email to