in reply to RE: List processing (benchmark results)
in thread List processing performance

I ran this myself, and visnu's code is broken in your benchmarking. visnu's code is checking the %common hash for the common words, and you don't have such a hash defined. Since the other benchmarked code has setup code within the benchmarks (setting up the common words hash), we should do that for visnu's, so add this at the beginning:
my %common; @common{@common} = (1) x @common;
Also, the PMGurus code doesn't work quite right, as it doesn't lower-case the words in the grep.

After taking out the PMGurus code, and fixing up visnu's, I got:

Benchmark: timing 1000 iterations of Odud, btrott, visnu... Odud: 4 secs ( 3.55 usr 0.00 sys = 3.55 cpu) btrott: 2 secs ( 2.27 usr 0.00 sys = 2.27 cpu) visnu: 4 secs ( 3.98 usr 0.00 sys = 3.98 cpu)

Replies are listed 'Best First'.
RE:(3) List processing (benchmark results)
by Russ (Deacon) on Jul 13, 2000 at 01:09 UTC
    I made the changes you mentioned. I had missed visnu's %common, and your code and PMGurus code ignored the common words, in my version. Also, since the PMGurus code does lower-case the words in the grep, I have included those results.

    Thanks for pointing out what I had missed.

    Here is the latest code, and results:

    @common = qw|a an and at the to|; timethese(1000, { Odud => q{ my (@only, %r, %seen); foreach $r (@words){ $r{lc $r} = 1; } my @uniqwords = sort keys %r; foreach my $item (@uniqwords) { push(@only,$item) unless exists $seen{$item}; } my @newwords = @only; }, PMGurus => q{ my %seen; @seen{@common} = (1) x @common; my @newwords = sort grep !$seen{+lc}++, @words; }, btrott_OneLiner => q{ my %seen; @seen{@common} = (1) x @common; my @newwords = sort grep !$seen{$_=lc}++, @words; }, btrott_FirstPass => q{ my (%seen, %r); @seen{@common} = (1) x @common; for my $r (@words) { next if exists $seen{ lc $r }; $r{lc $r} = 1; } my @newwords = sort keys %r; }, visnu => q{ my %common; @common{@common} = (1) x @common; my @newwords = sort keys %{+{ map { !$common{$_ = lc $_} ? ($_, 1) + : () } @words }}; }, }); Benchmark: timing 1000 iterations of Odud, PMGurus, btrott_FirstPass, +btrott_OneLiner, visnu... Odud: 9 wallclock secs ( 9.61 usr + 0.00 sys = 9.61 CPU) PMGurus: 6 wallclock secs ( 5.40 usr + 0.00 sys = 5.40 CPU) btrott_FirstPass: 7 wallclock secs ( 7.07 usr + 0.00 sys = 7.07 CPU) btrott_OneLiner: 6 wallclock secs ( 5.73 usr + 0.00 sys = 5.73 CPU) visnu: 10 wallclock secs (10.29 usr + 0.00 sys = 10.29 CPU)
    Russ
    Brainbench 'Most Valuable Professional' for Perl