(btw, don't you mean sub blah instead of my blah)
Benchmark it!
use Benchmark qw( cmpthese );
$i=0;
sub blah1 {
my @list = qw(# list of things);
foreach my $item (@list) { $i++; }
}
{
my @list;
BEGIN {
@list = qw(# list of things);
}
sub blah2 {
foreach my $item (@list) { $i++; }
}
}
sub blah3 {
foreach my $item qw(# list of things) { $i++; }
}
cmpthese(0, {
blah1 => \&blah1,
blah2 => \&blah2,
blah3 => \&blah3,
});
__END__
(partial) output
================
Rate blah1 blah2 blah3
blah1 118024/s -- -52% -53%
blah2 245479/s 108% -- -1%
blah3 248532/s 111% 1% --
2 and 3 are equally fast, so you might as well use the cleaner 3.
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.