For this simple program, using first() instead of (scalar grep) leads to a 47-fold increase in searching these lists of lists for a single value.
use Benchmark; use List::Util qw(first); use strict; my @big_array = (1 .. 20); my %x; $x{$_} = \@big_array for (1 .. 100); sub search_5_6 { scalar grep { scalar grep { $_ == 5 } @$_; } values %x; } sub search_5_7 { # Scalar-List-Utils is part of 5.7.2 and onward first { first { $_ == 5 } @$_; } values %x; } timethese(10000, { 5.6 => \&search_5_6, 5.7 => \&search_5_7 } ); =head1 Benchmark: timing 10000 iterations of 5.6, 5.7... 5.6: 47 wallclock secs (32.57 usr + 0.00 sys = 32.57 CPU) @ 30 +7.03/s (n\ =10000) 5.7: 1 wallclock secs ( 0.57 usr + 0.00 sys = 0.57 CPU) @ 17 +543.86/s \ (n=10000) =cut
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: List::Util::first in Scalar-List-Utils distribution
by japhy (Canon) on Aug 04, 2001 at 00:05 UTC | |
|
Re: List::Util::first in Scalar-List-Utils distribution
by princepawn (Parson) on Aug 04, 2001 at 00:27 UTC |