in reply to Re: Re: Beauty is in the eye of the beholder
in thread Beauty is in the eye of the beholder

Here's my test. first() can be made faster by passing an array reference, not the array.
(RESULTS) Elements:17576 sub took: 52 (51.14 usr + 0.00 sys = 51.14 CPU) scan took: 17 (13.85 usr + 0.00 sys = 13.85 CPU) hash took: 0 ( 0.17 usr + 0.13 sys = 0.30 CPU) (CODE) #!/usr/bin/perl -w use Benchmark; @List = ("aaa" .. "zzz"); print "Elements:", scalar @List, "\n"; @rand_list = map $List[rand @List], 1 .. 100; sub first (&@) { my $cref = shift; $cref->($_) and return $_ for @_; } $t0 = new Benchmark; for (@rand_list) { $rand_element = $_; $first_match = first {$_ eq $rand_element } @List; } $t1 = new Benchmark; $td = timediff($t1, $t0); print "sub took: ", timestr($td), "\n"; $t0 = new Benchmark; for (@rand_list) { $rand_element = $_; for (@List) { last if $_ eq $rand_element } } $t1 = new Benchmark; $td = timediff($t1, $t0); print "scan took: ", timestr($td), "\n"; $t0 = new Benchmark; my %seen; @seen{@List} = (); $t1 = new Benchmark; $td = timediff($t1, $t0); for (@rand_list) { 1 if exists $seen{$_} } print "hash took: ", timestr($td), "\n";


japhy -- Perl and Regex Hacker