Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Is it wrong?

by TomDLux (Vicar)
on Aug 27, 2003 at 17:33 UTC ( [id://287102]=note: print w/replies, xml ) Need Help??


in reply to Is it wrong?

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://287102]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2024-03-28 15:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found