Is there a problem with its efficiency? I mean is it running unacceptably slowly (assuming the efficiency you seek is speed)? Is it consuming too much memory?

You could avoid slurping the file by building @cmpstartinstall and @cmpendinstall on the fly while iterating over the lines of the file. Currently you're slurping the file (which is an unseen loop), then grepping the file for @cmpstartinstall (a second implicit loop), and then grepping the file for @cmpendinstall (a third implicit loop). If you iterated over the file line by line and used push @cmpstartinstall if m/xxxxxxxxxxxxxx/, along with push @cmpendinstall if m/yyyyyyyyyyy/, you reduce the three implicit loops to one explicit one. That way, you're essentially iterating over the file only one time; the time you read it and push lines into @cmpstartinstall and @cmpendinstall.

But although that may be a more efficient way of handling it (from both the standpoint of memory usage, as well as processor cycles), the gains will not be significant if the file is only a hundred or so lines long. If, in your current version, the time required to read in the file is 1/10th of a second, and the time required to grep through it twice is 1/10th of a second each time, for a grand total of 3/10ths of a second, with a total runtime including startup compilation/interpretation of 1.5 seconds, and the process runs once a week, do you care if you can reduce the execution portion of the script from 3/10ths to 1/10th of a second, with a total footprint of 1.2 seconds once a week?

On the other hand, if your logfile is millions of lines long, and your current solution bogs down the system by slurping the file and iterating over it three times, and the script takes 2 minutes to execute with a frequency of 5 minutes, you've got a significant problem.

The first example isn't worth fiddling with except to satisfy curiosity and gain some practice in "best practices." The second example would require entirely rethinking the problem, starting with devising a means of caching results to prevent the need to parse the whole file so frequently.

Don't get me wrong; I enjoy challenging myself to find more efficient ways of doing things. But from a practical standpoint, you may not really be gaining much.


Dave


In reply to Re: Need to optimize my perl code by davido
in thread Need to optimize my perl code by achak01

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.