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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.