I write a large amount of code to handle stupily large amounts of data at work, and I find myself spending a lot of my time while designing perl scripts thinking not only about how to do something, but getting bogged down in the quagmire of "how best to do something".

This largely (at the moment) comes down to trial and error for me - which is *very* frustrating - hence this Meditation .. me trying to put thoughts down on paper - scary thought .. :P I'm sure this is something that comes up a lot .. but I thought I'd post it anyway, and see what comes of it.

Perl is flexible. Very flexible. To the point that, for any given task, you can all but guarantee that there's a completely different, and probably better, way to do it.

This is one of Perl's best features - but also one of it's most frustrating. Even with the most carefully crafted code, I find myself wondering if I could have done it some other way to shave valuable minutes off of processing the million-or-so data records coming out of the database.

Bizarrely, to me at least, coming from a C background, where there's rarely more than one "recommended" way to do something, few of the "good" Perl books seem to touch on code effiency, barring a quick "there's more than one way to do something" comment in the introductory paragraph/chapter of the book.

Surely for something this fundamental - runtimes of scripts can be multipled many times with a less "efficient" method of processing - some guidelines should exist - even if it were just a quick "tip-sheet" to give you a helping hand when figuring out how not to write a script.

I can see that there's really no restrictions on how code is written - and it's largely a matter of personal style, preference and experience ..

What I'm interested to know is ...
- how do other Monks go about optimising their code?
- when faced with more than one obvious way to do something, what's the determining factor?
- surely this has to be more than "trial and error" ..? *grin*

Anyway .. enough of my thoughts/contemplations/moaning .. just thought I'd see what people thought.
--Foxcub


In reply to 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.