Glenn Linderman wrote:
> On approximately 4/19/2006 5:42 PM, came the following characters from
> the keyboard of Sisyphus:
>
>>----- Original Message -----
>>From: "Glenn Linderman"
>>.
>>.
>>
>>>I think that the
>>>
>>>for( grep ($_ != 3, @a))
>>>
>>>is quite clear in bundling the element selection together, and
>>>separating it from the functions being performed.
>>>
>>
>>I would much rather see (as suggested earlier on in this thread):
>>
>>for(@a) {
>> next if $_ == 3;
>>
>>For me it is both clearer and more efficient ... but each to his own.
>>
>>On the subject of replacing brackets with modifiers (which I think was also
>>raised earlier on), I was surprised to find that using a modifier is about
>>25% faster than brackets:
>>
>>
>>### try.pl ###
>>use warnings;
>>no warnings "once";
>>use Benchmark;
>>
>>@x = (1 .. 1000000);
>>@y = (1 .. 1000000);
>>
>>$z1 = 0;
>>$z2 = 0;
>>
>>timethese(1, {
>>'modifier' => 'for(@x) {$z1++ if $_ != 3}',
>>'brackets' => 'for(@y) {if($_ != 3){$z2++}}',
>>});
>>
>>print "\n", $z1, " ", $z2, "\n";
>>__END__
>>
>>D:\pscrpt>perl try.pl
>>Benchmark: timing 1 iterations of brackets, modifier...
>> brackets: 1 wallclock secs ( 1.03 usr + 0.00 sys = 1.03 CPU) @ 0.97/s
>>(n=1)
>> (warning: too few iterations for a reliable count)
>> modifier: 1 wallclock secs ( 0.70 usr + 0.00 sys = 0.70 CPU) @ 1.43/s
>>(n=1)
>> (warning: too few iterations for a reliable count)
>>
>>999999 999999
>>
>>Maybe I've misinterpreted the results ??
>>
>>Cheers,
>>Rob
>
>
> I get similar, but I guess my new computer is faster than yours, so I
> did 10 iterations... and got 3.77 for brackets, and 2.44 for modifier.
> Both your number and mine are closer to 30% than 25% faster. Interesting.
>
> Get a load of this variation:
>
> perl
> use warnings;
> no warnings "once";
> use Benchmark;
>
> @x = (1 .. 1000000);
> @y = (1 .. 1000000);
> @z = (1 .. 1000000);
> @w = (1 .. 1000000);
>
> $z1 = 0;
> $z2 = 0;
> $z3 = 0;
> $z4 = 0;
>
> timethese(10, {
> 'forgrep' => 'for(grep($_ != 3, @w)){ $z4++ }',
> 'next' => 'for(@z) {next if $_ == 3; $z3++}',
> 'brackets' => 'for(@y) {if($_ != 3){$z2++}}',
> 'modifier' => 'for(@x) {$z1++ if $_ != 3}',
> });
>
> print "\n", $z1, " ", $z2, " ", $z3, " ", $z4, "\n";
> __END__
>
> Benchmark: timing 10 iterations of brackets, forgrep, modifier, next...
> brackets: 4 wallclock secs ( 3.73 usr + 0.00 sys = 3.73 CPU) @
> 2.68/s (n=10)
> forgrep: 3 wallclock secs ( 3.19 usr + 0.00 sys = 3.19 CPU) @
> 3.14/s (n=10)
> modifier: 3 wallclock secs ( 2.61 usr + 0.00 sys = 2.61 CPU) @
> 3.83/s (n=10)
> next: 3 wallclock secs ( 2.64 usr + 0.00 sys = 2.64 CPU) @
> 3.79/s (n=10)
Don't know how you could get that to run on only 10 iterations.
I ran 10 million iterations and got:
timethese (slight format mod to fit line):
Benchmark: timing 10000000 iterations of brackets, forgrep, modifier, next...
brackets: 9 secs ( 4.91 usr + 0.00 sys = 4.91 CPU) @ 2037905.03/s (n=10000000)
forgrep: 24 secs (12.20 usr + 0.00 sys = 12.20 CPU) @ 819470.62/s (n=10000000)
modifier: 10 secs ( 4.80 usr + 0.02 sys = 4.81 CPU) @ 2077706.21/s (n=10000000)
next: 10 secs ( 4.81 usr + 0.00 sys = 4.81 CPU) @ 2078137.99/s (n=10000000)
cmpthese 10000000:
Rate forgrep next modifier brackets
forgrep 810110/s -- -60% -61% -62%
next 2045408/s 152% -- -1% -4%
modifier 2071251/s 156% 1% -- -3%
brackets 2133561/s 163% 4% 3% --
> 9999990 9999990 9999990 9999990
>
>
>
> I have to admit that if the complexity of the filter expression was more
> than one comparison or match, that I'd rather see them in the body of
> the loop, too. But for one comparison, I kind of like the for-grep.
>
--
,-/- __ _ _ $Bill Luebkert Mailto:[EMAIL PROTECTED]
(_/ / ) // // DBE Collectibles Mailto:[EMAIL PROTECTED]
/ ) /--< o // // Castle of Medieval Myth & Magic http://www.todbe.com/
-/-' /___/_<_</_</_ http://dbecoll.tripod.com/ (My Perl/Lakers stuff)
_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs