On 7/18/07, Paul Johnson <[EMAIL PROTECTED]> wrote:
snip
I see the following options:

 - split your code into subroutines to gain a better understanding of
   what is taking how much time

 - install Devel::Cover and run
     $ perl -MDevel::Cover ./analyze.pl traplas-example output
     $ cover
   to gain a finer grained but less accurate profiling report

 - go back to guessing - you were probably right ;-)

 - post your code so that other people can guess too

 - buy more hardware
snip

I second most of these recommendations, but looking at your code I
spot a pattern of inefficient code: you keep looping over the same
string.

 385         foreach my $stat_key (@stat_avg_keys) {
 386           if($line =~ m/^\s*$stat_key$stat_pat/) {
snip
 399            foreach my $stat_key (@stat_var_keys) {
 400              if($line =~ m/^\s*$stat_key$stat_pat/) {
snip
 406         foreach my $stat_key (@stat_sum_keys) {
 407           if($line =~ m/^\s*$stat_key$stat_pat/) {
snip
 413            foreach my $stat_key (@stat_min_keys) {
 414              if($line =~ m/^\s*$stat_key$stat_pat/) {
snip
 420            foreach my $stat_key (@stat_max_keys) {
 421           if($line =~ m/^\s*$stat_key$stat_pat/) {
snip
 427         foreach my $stat_key (@stat_cnt_keys) {
 428           if($line =~ m/^\s*$stat_key$stat_pat/) {


You can fix this by changing the arrays above to hashes whose keys are
the values in the array and the values are all 1.  This allows the
hash to function as a quick lookup.  Then change the code to read the
line into a hash of keys and values and loop over the keys checking to
see if the key is a certain type.  Here is some example code:

#!/usr/bin/perl

use strict;
use warnings;

my %cnt_keys = (
       foo => 1
);
my %min_keys = (
       foo => 1,
       baz => 1
);

$_ = "foo 100 1e99 nan nan bar 1e99 -inf inf baz 1 2 3 4";

my %rec;
my $identifier = qr/[A-Za-z_]\w*/;
my $expression = qr/nan|-?inf|[0-9\.e\+\-]+/;
while (/($identifier) ((?: \s+ $expression)+) \s*/gx) {
       $rec{$1} = [ split ' ', $2 ];
}

for my $key (keys %rec) {
       print "$key is\n";
       if ($cnt_keys{$key}) {
               print "\ta cnt_key with value $rec{$key}[0]\n";
       }
       if ($min_keys{$key}) {
               print "\ta min_key with a value of $rec{$key}[1]\n";
       }
}

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to