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:

  1. Three occurrences of $HoH{$p} =  {%rec}; could become $HoH{$p} =  \%rec; without affecting the program, but avoiding replication of existing hashes.
  2. One occurance of  push @data, {%HoH}; can become  push @data, \%HoH; for the same reason.

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.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

In reply to Re: need to optimize my sub routine by BrowserUk
in thread need to optimize my sub routine by convenientstore

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.