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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |