List::Util is a part of 5.7.2 but not yet part of the 5.6 tree. It has several list processing functions coded in C that are useful.

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
    Your outer block is doing nothing.
    scalar grep { ??? == scalar grep { ... } @$_ } values %hash; first { ??? == first { ... } @$_ } values %hash;
    You need something meaningful in the ??? area. The reason first is so fast there is because you're merely testing to see if some number (specifically, the number returned by the innermost first) is true.

    _____________________________________________________
    Jeff japhy Pinyan: Perl, regex, and perl hacker.
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Re: List::Util::first in Scalar-List-Utils distribution
by princepawn (Parson) on Aug 04, 2001 at 00:27 UTC
    The problem spec is: "return true if any value in any of the array refs that are values in the hash are equal to 5"

    I'm not sure I agree with you and here's why: we need to short-circuit the iteration over values %x as well short-circut the iteration over the array ref in each of the respective values.