>>>>> "AM" == Asa Martin <asa.mar...@gmail.com> writes:

  AM> my ($line, @rules);

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

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

  AM> Here were my proposed changes:

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

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

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

a classic case of premature optimization. the speed or ram difference
would likely be way less than the time you waste thinking about
this. the code style is more important that trivial little speedups (if
any). the rule is to declare variables in the tightest scope possible so
your version is better (along with having named vars vs hardwired
indexes).

also note that perl doesn't free up lexicals immediately so they could
be reused in the next loop. declaring stuff outside a loop is not a real
win but only benchmarking can tell (and isn't worth it here IMNSHO).

now, depending on how large the file is, slurping it may be faster than
reading it line by line (larger files will win more). the reason is
perl's stream i/o is slower than a single slurp. also you could chomp
the whole file in one call. that alone could make up any difference you
would get in any declaration changes. the real trick to perl speed is
reducing perl calls and staying inside of perl's guts (which is in
c). the perl op loop is about the largest overhead of all.

uri

-- 
Uri Guttman  ------  u...@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------

_______________________________________________
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm

Reply via email to