in reply to Too much SQL not enough perl
#!/usr/bin/perl -w use strict; use Benchmark qw(:all); my @choices = qw|a b c|; my %choices = map{$_=>1}@choices; my @cData = <DATA>; timethese(100000,{'Damn Slow'=>\&parseData1, 'Much Better'=>\&parseData2}); sub parseData1 { foreach (@cData){ chomp; my @items = split/,/,$_; #slow way, foreach my $item (@items){ if(grep {$item eq $_} @choices){ #print qq|FOUND $item|; } } } } sub parseData2 { foreach(@cData){ chomp; foreach my $item (split/,/,$_){ if ($choices{$item}){ #print qq|FOUND $item\n|; } } } } __DATA__ z,t,m,u,a,b,c s,t,l,m,z,a,s c,b,a,m,u,t,n k,l,t,s,z,r,t
Produces:
Benchmark: timing 100000 iterations of Damn Slow, Much Better... Damn Slow: 9 wallclock secs ( 8.81 usr + 0.00 sys = 8.81 CPU) @ 11 +348.16/s n=100000) Much Better: 4 wallclock secs ( 3.88 usr + 0.00 sys = 3.88 CPU) @ 2 +5806.45/s (n=100000)
Celebrate Intellectual Diversity
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Too much SQL not enough perl
by EvanCarroll (Chaplain) on Oct 10, 2005 at 04:29 UTC | |
by Roy Johnson (Monsignor) on Oct 10, 2005 at 13:33 UTC | |
by Aristotle (Chancellor) on Oct 10, 2005 at 14:48 UTC | |
by Roy Johnson (Monsignor) on Oct 10, 2005 at 14:58 UTC | |
by Aristotle (Chancellor) on Oct 10, 2005 at 15:24 UTC | |
by InfiniteSilence (Curate) on Oct 10, 2005 at 15:10 UTC |