Don't be a litterbug

Don't waste memory just because it seems computers have large memory spaces. Similarly, don't abuse CPU cycles, intentionally use bad algorithmns, ....

Comprehensible code is always the priority, but keep an eye out for opportunities for efficiency; avoid wasteful techniques. In particular, avoid things that are silly, wasteful, meaningless, and don't contribute to effectiveness. I am especially irritated when programmers pass an array or hash reference into a routine, then copy it to a real array or hash. If the object is known always to be small, you could consider copying the whole thing into the routine.... but what if someone decides to use your routine for a much larger data set. Instead of copying data, learn to use the arrow to dereference array references. It isn't so difficult!


Don't sweat the small stuff

If it's never going to affect more than 0.01%, who cares how efficient or inefficient it is?


Figure out your priorities

Let's say an object/structure takes up 128 bytes. If we have a loop which iterates a million times, options include:

  1. allocate, use,and deallocate a million objects
  2. allocate, and use a million objects without deallocating them
  3. allocating one object, using it a million times to store different data, then de-allocate once.

Option 1 involves a million allocations and a million de-allocations, all of which take time, but requires only 128 bytes at any one time.

Option 2 takes a million allocations and no de-allocations, so that saves some time. On the other hand, it requires 128,000,000 bytes of memory. How much time will be used in swapping memory to disk?

Option 3 uses only a single alloaction and deallocation, and takes only 128 bytes, but there is a risk for unclear code in the procecss of saving memory.


Don't optimize too early

The first priority is to develop a program which is complete and correct. Then you can profile the system to consider whether it could be better.

--
TTTATCGGTCGTTATATAGATGTTTGCA


In reply to Re: Is it wrong? by TomDLux
in thread Is it wrong? by Drgan

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.