If the question is which is faster, then one could pontificate endlessly about potential permutations of perl compiler patterns that may or may not come to pass.

Or.

One could download one of the myriad of benchmakrking modules and let objective data win over various perl myths.

Greg

-----Original message-----
From: Michael Small <[email protected]>
To: Asa Martin <[email protected]>
Cc: [email protected]
Sent: Fri, Feb 4, 2011 22:37:15 GMT+00:00
Subject: Re: [Boston.pm] Question on optimization/memory allocation

Asa Martin <[email protected]> writes:
...
my ($line, @rules);

while ($line = <FH>) {
    chomp $line;
    @rules = split("\t", $line);

.... do stuff with $rules[0], $rules[1], $rules[2] and $rules[3] ...

Here were my proposed changes:

while (my $line = <FH>) {
    chomp $line;
    my ($domain, $subdomain, $rule, $label) = split("\t", $line);

.... do stuff with $domain, $subdomain, $rule and $label ....

I was told that "predeclaring" the variables outside the loop saved on
memory allocation, and that using @rules instead of four named variables
was
also more efficient. I had never considered that this could be the case,
and
said I didn't think this was true, but didn't really know.

Theoretically, I could imagine your version being faster since you're
giving the compiler more information about the scope and lifetime of the
variables involved.  Perhaps perl could make better decisions about
register usage (I don't know perl internals).  I guess they're thinking
the variables are created anew each loop iteration (which is what, one
increment of a frame pointer or would that hit the heap, assuming it's
not optimized out completely?).

Someone should measure.

Your way is plainly better for readability and maintenance.

- Mike

_______________________________________________
Boston-pm mailing list
[email protected]
http://mail.pm.org/mailman/listinfo/boston-pm


_______________________________________________
Boston-pm mailing list
[email protected]
http://mail.pm.org/mailman/listinfo/boston-pm

Reply via email to