Since I just updated my previous suggestion I figured I would Benchmark things...
Not that it matters but I thought
you might be interested.
Update There was an issue with how I ran the test before. Here are the updated code and results.
#!/usr/bin/perl -w
use strict;
use Benchmark;
my $count = 500000;
my $line = "1!2!3!4!5!6!7!8!9";
my @arr = split /!/, $line;
my @exclude = (2,5..8);
my @exclude_lkup;
$exclude_lkup[$_] = 1 for @exclude;
sub ikegami
{
my @tarr = @arr;
my @filtered = map $tarr[$_], grep !$exclude_lkup[$_], 0..$#tarr;
}
sub injun
{
my @tarr = @arr;
@tarr[@exclude] = ();
@tarr = grep $_, @tarr;
}
timethese (
$count,
{'Ikegami' => '&ikegami',
'InjunJoel' => '&injun'}
);
Results in
Benchmark: timing 500000 iterations of Ikegami, InjunJoel...
Ikegami: 16 wallclock secs (15.88 usr + 0.00 sys = 15.88 CPU) @ 314
+94.08/s (n=500000)
InjunJoel: 10 wallclock secs ( 11.05 usr + 0.00 sys = 11.05 CPU) @ 4
+5265.25/s (n=500000)
Though I'm not versed enough in O(n) notation to tell you why...
-InjunJoel
"I do not feel obliged to believe that the same God who endowed us with sense, reason and intellect has intended us to forego their use." -Galileo
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.