in reply to Re: Fastest way to test membership in a constant set (don't use a hash)
in thread Fastest way to test membership in a constant set
Not the way I tested it?
#! perl -slw use strict; use Hash::Util qw[ lock_hash unlock_hash ]; use Benchmark qw[ cmpthese ]; our $ITERS ||= -1; our @items = qw[ age old years ]; our @tests = ( @items, map{ $_ . 'x' } @items ); our %set = map{ $_ => 1 } @items; lock_hash %set; our $set = $; . join( $;, @items ) . $;; cmpthese $ITERS, { hash => q[ my $count; exists $set{ $_ } and ++$count for @tests; print "hash: $count" if $ITERS == 1; ], index => q[ my $count; 1+index $set, $; . $_ . $; and ++$count for @tests; print "index: $count" if $ITERS == 1; ], }; @items = map{ $_ x 5 } 'a' .. 'z'; @tests = ( @items, map{ $_ . 'x' } @items ); unlock_hash %set; %set = map{ $_ => 1 } @items; lock_hash %set; $set = $; . join( $;, @items ) . $;; cmpthese $ITERS, { hash => q[ my $count; exists $set{ $_ } and ++$count for @tests; print "hash: $count" if $ITERS == 1; ], index => q[ my $count; 1+index $set, $; . $_ . $; and ++$count for @tests; print "index: $count" if $ITERS == 1; ], }; __END__ ## Check they produce teh same results: C:\test>junk3 -ITERS=1 hash: 3 (warning: too few iterations for a reliable count) index: 3 (warning: too few iterations for a reliable count) Rate hash index hash 1000000000000000/s -- 0% index 1000000000000000/s 0% -- hash: 26 (warning: too few iterations for a reliable count) index: 26 (warning: too few iterations for a reliable count) Rate hash index hash 1000000000000000/s -- 0% index 1000000000000000/s 0% -- ## See how fast C:\test>junk3 Rate index hash index 166820/s -- -31% hash 240174/s 44% -- Rate index hash index 18301/s -- -59% hash 44377/s 142% --
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Fastest way to test membership in a constant set (Why not?)
by hipe (Sexton) on Jul 31, 2007 at 12:09 UTC | |
by BrowserUk (Patriarch) on Jul 31, 2007 at 14:33 UTC | |
by hipe (Sexton) on Aug 01, 2007 at 13:55 UTC |