Interestingly, benchmarks do not bear this assumption out. (perl 5.8.0 on a celeron lots of other stuff going on at the same time, but tried multiple times, very similar results).
#!/usr/local/bin/perl
use Benchmark;
for (1.. 100000) {
$rand = rand * 100;
push @rvs, $rand;
$last = $rand;
}
$array[0] = $rand;
timethese ( 100, {grepit => \&grepit,
hashit => \&hashit});
sub grepit {
return 1 if (grep /$array[0]/, @rvs);
}
sub hashit {
@hash{@rvs} = (1) x @rvs;
return 1 if ($hash{$array[0]});
}
Benchmark: timing 100 iterations of grepit, hashit...
grepit: 19 wallclock secs
(19.36 usr + 0.03 sys = 19.39 CPU) @ 5.16/s (n=100)
hashit: 28 wallclock secs
(27.84 usr + 0.04 sys = 27.88 CPU) @ 3.59/s (n=100)
the number is 0.359661571026372
Grepit found the number
Hashit found the number
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.