That subroutine has large amounts of cut&paste code that could be reduced, but mostly that wouldn't affect the performance.
Two changes that might improve it are:
This is total speculation given the number of variables used within this sub but not declared there, so take it with a pinch of salt, but the subroutine may be reducable to something along the lines of:
sub main { my $csv = Text::CSV_XS->new; for( @files ) { open( NOW , "$directory/$_" ) || die "you suck\n"; while( <NOW> ) { chomp; my( %rec, %HoH, $p ); $t_counter++; my( $attrsRef, @selectedFields ) = /^STOP/ ? ( \@attrs_sto, 0,1,13,14,16,20,33,34,36, +67 ) : /^START/ ? ( \@attrs_sta, 0,1,11,15,28,29,31,53 + ) : /^ATTEMPT/ ? ( \@attrs_att, 0,1,11,13,17,30,31,33,57 + ) : $ncounter++; ; my %rec{ @$attrs_ref ) = @fields{ @selectedFields ); my $p = $rec{ _i_pstn_trunk } ? extract( $rec{ _i_pstn_circuit + }, $rec{ _i_pstn_trunk } ) : $rec{ _e_pstn_trunk } ? extract( $rec{ _e_pstn_circuit + }, $rec{ _e_pstn_trunk } ) : $ncounter++ ; $csv->parse( $_ ); my @fields = $csv->fields; push @data, \%HoH; } close NOW; } }
The main unknown is the significance of the variable $ncounter? (BTW: Naming a variable $somethingcounter is ... That it is a counter is obvious from it's usage, but one letter to signify what it is counting, when it is declared at a higher level, does not convey much information.
On similar lines, having arrays called @attrs_sto, @attrs_sta, and @attrs_att, presumably STOP, START and ATTEMPT, minimises the important information (to just one character sometimes) and maximises the less important common element.
In reply to Re: need to optimize my sub routine
by BrowserUk
in thread need to optimize my sub routine
by convenientstore
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |