in reply to Resource allocation question

(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.

Replies are listed 'Best First'.
Re^2: Resource allocation question
by shemp (Deacon) on Oct 20, 2004 at 20:06 UTC
    Ok good. Although the list in #3 never gets assigned to a variable, the interpreter certainly knows about it. So i was thinking about it in an analogous manner to when you have something like:
    if ( $x =~ /#some regex/o ) { # or if ( $x =~ /#some constant regex/ ) {
    Those regex's are only compiled once, whatever that means. I guess it means that the interpreter stores the info away somewhere to be readily available for use later.