Q1) how do other Monks go about optimizing their code?
Q2) when faced with more than one obvious way to do something, what's the determining factor?
Q3) surely this has to be more than "trial and error" ..? *grin*

A1) Since reading between the lines it seems you are working with a database most of the time, there is much more to getting efficient code then just the Perl part of it. You need to be concerned with the network (if it is a remote database), the indexing of the tables, this can make a big difference on the speed of your code. In one recent project I got a ten fold difference in speed on a report by adding the appropriate indexes to the tables. As far as Perl efficiency is concerned this also very dependent on the type of data you are dealing with. I recommend you use a profiler to find out where the most time is being spent in you code. You might want to checkout Devel::AutoProfiler .

A2) For me unless there is a known efficiency issue I code to my level as much as possible. I occasionally will use code outside of my comfort zone in the hopes of adding it at some point to my bag of tricks, but some code just doesn't "sound" right and makes more of a maintenance issue then anything else. Another thing to consider is Lifespan of the code in question. If this is a one time or seldom run piece of code you most likely won't recover from the time "wasted" in making more efficient. Optimize only when it is beneficial in the grand scheme of things.

A3) Using different benchmarking tools and code profiling you can determine what is causing the bottleneck and code those sections differently.


In reply to Re: Code Efficiency: Trial and Error? by trs80
in thread Code Efficiency: Trial and Error? by Tanalis

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.