Tip 1: profile. There's no real point in optimising code which already contributes negligibly to the runtime. I like the venerable Devel::NYTProf but other profilers are available.

I haven't analysed your code in any detail but this just jumps right out at me:

for (@lines2) { s/A2022Local/ACTUAL;FY22;Working_Central;Input;Loc +al_YTD/g } for (@lines2) { s/A2022AjConsoLocal/ACTUAL;FY22;Working_Central;Ad +j_Conso;Local_YTD/g } for (@lines2) { s/A2022InEur/ACTUAL;FY22;Working_Central;Input;Eur +_ACT2022_Rate_YTD/g } for (@lines2) { s/A2022AjConso/ACTUAL;FY22;Working_Central;Adj_Con +so;Eur_ACT2022_Rate_YTD/g } for (@lines2) { s/A2022TxB22/ACTUAL;FY22;Working_Central;Input;Eur +_BUD2022_Rate_YTD/g } for (@lines2) { s/A2022TxB22AjConso/ACTUAL;FY22;Working_Central;Ad +j_Conso;Eur_BUD2022_Rate_YTD/g } for (@lines2) { s/A2021Local/ACTUAL;FY21;Working_Central;Input;Loc +al_YTD/g } for (@lines2) { s/A2021AjConsoLocal/ACTUAL;FY21;Working_Central;Ad +j_Conso;Local_YTD/g } for (@lines2) { s/A2021InEur/ACTUAL;FY21;Working_Central;Input;Eur +_ACT2021_Rate_YTD/g } for (@lines2) { s/A2021AjConso/ACTUAL;FY21;Working_Central;Adj_Con +so;Eur_ACT2021_Rate_YTD/g } for (@lines2) { s/A2021TxB22/ACTUAL;FY21;Working_Central;Input;Eur +_BUD2021_Rate_YTD/g } for (@lines2) { s/A2021TxB22AjConso/ACTUAL;FY21;Working_Central;Ad +j_Conso;Eur_BUD2021_Rate_YTD/g } for (@lines2) { s/A2020Local/ACTUAL;FY20;Working_Central;Input;Loc +al_YTD/g } for (@lines2) { s/A2020AjConsoLocal/ACTUAL;FY20;Working_Central;Ad +j_Conso;Local_YTD/g } for (@lines2) { s/A2020InEur/ACTUAL;FY20;Working_Central;Input;Eur +_ACT2020_Rate_YTD/g } for (@lines2) { s/A2020AjConso/ACTUAL;FY20;Working_Central;Adj_Con +so;Eur_ACT2020_Rate_YTD/g } for (@lines2) { s/A2020TxB22/ACTUAL;FY20;Working_Central;Input;Eur +_BUD2020_Rate_YTD/g } for (@lines2) { s/A2020TxB22AjConso/ACTUAL;FY20;Working_Central;Ad +j_Conso;Eur_BUD2020_Rate_YTD/g } for (@lines2) { s/B2022Local/BUDGET;FY22;Working_Central;Input;Loc +al_YTD/g } for (@lines2) { s/B2022AjConsoLoc/BUDGET;FY22;Working_Central;Adj_ +Conso;Local_YTD/g } for (@lines2) { s/B2022AjTBLocal/BUDGET;FY22;Working_Central;Adj_C +onso;Local_YTD/g } for (@lines2) { s/B2022InEur/BUDGET;FY22;Working_Central;Input;Eur +_BUD2022_Rate_YTD/g } for (@lines2) { s/B2022AjConso/BUDGET;FY22;Working_Central;Adj_Con +so;Eur_BUD2022_Rate_YTD/g } for (@lines2) { s/B2022AjTB/BUDGET;FY22;Working_Central;Adj_Conso; +Eur_BUD2022_Rate_YTD/g }

You are looping over the same array 24 times to run 24 very similar search/replace operations. It should be much more efficient (not to mention maintainable) to combine these into one loop and ideally one search/replace operation. But if your profiling indicates that the slow part is elsewhere, concentrate on that first.


🦛


In reply to Re: Optimization tips by hippo
in thread Optimization tips by sroux

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.