I have a block of code that looks like this:
my ($cap, %dest); # Open a file and slurp in the entire contents to one scalar open(CAP, "$file"); while(<CAP>) { $cap .= $_; } close CAP; # Scan through the contents for source/dest pairs and keep track of ho +w many times the dest gets hit: while($cap =~ /\s+(?:(?:\d{1,3}\.){3}\d{1,3})\s+->(\d{1,3}\.){3}\d{1,3 +})\s+/g) { unless(exists($dest{$1})) { $dest{$1} = 1; } else { $dest{$1}++; } } # Print out the destinations and the number of times they were hit in +descending order foreach my $dest_addr (sort { $dest{$b} <=> $dest{$a} } keys %dest) { print "$dest_addr\t$dest{$dest_addr}\n"; }
For those interested, the code is used to extract source/destination IP pairs from logs we use at the office...

The problem lies in that the code is called from as a CGI script, and when running over 20,000 lines of data (the SAME data on each run, mind you), the process will sometimes take only 5 seconds to run, and other times the process will take up to 5 minutes to run.

I realize that the actual execution times from run to run will be different based on how much activity is on the server at the time, but I really can't explain how parsing exactly the same file with exactly the same code can result in such drastically different execution times.

The remaining code surrounding this block is solid, I've done numerous Benchmark::Timer tests to granularly check each piece of my code, and literally, this is the only place where I get a jump in execution time, and it's only seen sometimes, but as far as I can tell, NOTHING is changing from run to run.

Based on what I've got above, can anyone see where I may be going wrong? Are there glaringly obvious Perl traps that I'm missing? This one really has me stumped....

Thanks in advance,
Tekkie

In reply to Same code, same data, different execution times? by tekkie

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.