in reply to Need to optimize my perl code
Is there an efficiency issue with the code? How many times too slow is the code?
Efficiency aside, there is plenty of scope for improving the code. As a first step use strictures (use strict; use warnings;). Then factor out the large lumps of repeated code into a sub. With a few other changes something like the following results:
use strict; use warnings; use Time::Local; my @data = <DATA>; my @component = qw(HPOvXpl HPOvSecCo HPOvBbc HPOvSecCC HPOvCtrl HPOvDe +pl HPOvConf HPOvPacc HPOvPerfMI HPOvGlanc HPOvPerfAgt HPOvPerlA HPOvAgtLc HPOvEaAgt HPOvOpsAgt ); install ($_) for @component; sub install { my ($component) = @_; my @cmpstartinstall = grep {/Installing the $component package/} @ +data; my @startime = map {/^\[(.+?)\]/; $1;} @cmpstartinstall; my $cmpstart = asEpoch ($startime[0]); my @endtime = map {/^\[(.+?)\]/; $1;} grep {/The component package $component installed successfully +/} @data; my $cmpend = asEpoch ($endtime[0]); return if ! defined $cmpstart || ! defined $cmpend; my $diff = $cmpend - $cmpstart; print "$component: $diff\n"; } sub asEpoch { my ($time) = @_; return if ! defined $time; return if $time !~ /^([\d]+)\/(\d+)\/(\d+)\s+(\d+):(\d+):(\d+)/; my ($mon, $mday, $year, $hour, $min, $sec) = ($1, $2, $3, $4, $5, +$6); $mon--; $year += 2000 if $year < 100; return timelocal ($sec, $min, $hour, $mday, $mon, $year); } __DATA__ ... [12/21/10 18:39:22] [oasetup] [oasetup] [INFO] Installing the HPOvXpl +package... ... [12/21/10 18:39:24] [oasetup] [oasetup] [INFO] The component package H +POvXpl installed successfully [12/21/10 18:39:24] [oasetup] [oasetup] [INFO] Installing the HPOvSecC +o package... ... [12/21/10 18:39:24] [oasetup] [oasetup] [INFO] The component package H +POvSecCo installed successfully
Prints:
HPOvXpl: 2 HPOvSecCo: 0
Note that the desired component is passed into the install sub and the entire log is processed for each call to install. That could easily be improved on, but significant changes would be needed to the code.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Need to optimize my perl code
by achak01 (Initiate) on Feb 26, 2011 at 12:07 UTC |