Hi monks, i've got the code below and it takes ages (>40 seconds) to run. I'm looking for ways to make it more efficient if possible.

The array @files is around 450 elements, and the file LOG is a large file containing tens of thousands of lines, which is then read into the string $log (this is faster than iterating through the lines of LOG):

local $/ = undef; open(LOG, 'log_file') or die("couldn't open file"); binmode LOG; $log = <LOG>; foreach $file (@files){ $file_counts{$file}[0] =()= $log =~ /<regexp1>/gi; $file_counts{$file}[1] =()= $log =~ /<regexp2>/gi; }

Can anyone suggest some improvements please?

EDIT: problem solved. I was doing it completely wrong by putting the log file into a string. the solution was to go through the logs in a loop HOWEVER in a different way. solutin is more or less outlined below:

while(<LOG>){ if(/GET\s.*\/(\S+)\.pdf\s/i and exists $file_counts{$1}){ $file_counts{$1}[0]++; } }

LOG file is an HTTP access log and the regex above matches HTTP requests for .pdf file types. So the following will have been a successful match and would be incremented in the hash.

GET /folder1/folder2/file.pdf HTTP ..etc

In reply to how to make a regex loop efficient? by mavili

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.