Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Efficient programming

by perlthirst (Scribe)
on Dec 22, 2008 at 06:32 UTC ( #731974=perlquestion: print w/replies, xml ) Need Help??
perlthirst has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,

I am writing a perl programs which always gives me a feeling that the method i am using is not efficient, there are lot more efficient method is available to achieve your requirements.

I think this is the right place to put this question..
How to write an efficient and effective program in perl? i.e Efficient and Effective means:
i) it should consume less amount of time, memory. it should be bug free. should be easily understandable.

Is there any link/book/article/discussion to get knowledge about how to write efficient programs in perl?
Kindly advice.

Replies are listed 'Best First'.
Re: Efficient programming
by CountZero (Bishop) on Dec 22, 2008 at 07:06 UTC
    Let's look at your requirements:
    • bug free: this has nothing to do with efficiency. A program which is not bug free, is just wrong. Of course for any but very simple programs you cannot be sure that there are no bugs lurking inside.
    • less time and memory use: These may be conflicting requrements. Perl easily trades memory for speed and with memory being cheap this is an acceptable trade-off. Now, neither speed nor memory use as such are matters to be too worried about. For example a script written for a one time task: I can make it run faster by 50% by reviewing the code and spending one hour of work. Is it worth the bother if it speeds-up the script from 1 minute to 30 seconds? Even big, repetitively run programs may be scheduled during night time and nobody will care if they take "too" long, as long as their job is done when the results are due.

      The same arguments go for memory consumption. Squeezing the last byte out of a memory structure may not be worth it. It all depends on what is required of it.

    • Easily understandable: if you mean by that "easy to maintain", I fully agree. But that is the most difficult part. Shy away from "too clever" constructs, use well tried and/or standard modules, comments and documentation are of course required and a full test suite (best written before you start to code) is a must.


    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: Efficient programming
by tilly (Archbishop) on Dec 22, 2008 at 09:06 UTC
    The path to mastery is an ancient node of mine that may shed some light on this. I would also recommend picking up Code Complete 2 and actually reading the whole thing. (Rather than, say, using it as a doorstop.) It is not bout Perl, but its advice pertains to programming Perl as well as any other language.
Re: Efficient programming
by monarch (Priest) on Dec 22, 2008 at 06:37 UTC

    Ah, grasshopper, you seek knowledge, but the time is not yet right to reveal this to you. First, you must learn patience!

    Meditate upon this now..

    Update: I should explain this comment: I believe all programmers seek to write the most optimal code, it is utopia! But it is a quest that takes us our natural lives and enlightenment comes with study and practice. The question being asked, i.e. how to do this in Perl, can be partially answered by learning any computer language (as others have mentioned below about gaining wisdom of algorithms and such). And specific understanding of efficiency in Perl can be gained by studying and utilising Perl itself - there are many such routes, including reading the Camel book, reading much of PerlMonks, and studying the source code of Perl itself!

    And so patience is required because the road is long.. take some initial steps and read as much as you can.

Re: Efficient programming
by ikegami (Pope) on Dec 22, 2008 at 06:48 UTC
    Efficient programs come from efficient algorithms. You'd best search for material on algorithms.

      OTOH, "Data dominates. If you've chosen the right data structures and organized things well, the algorithms will almost always be selfevident. Data structures, not algorithms, are central to programming." (Rob Pike Notes on Programming in C)

      As for the OP, "efficient" is a buzzword. Make your programming simple, concise, clear, and correct. For some simple guidelines, try that URL I pasted above, it's the best article about programming practices I know.

      "A core tenant of the greater Perl philosophy is to trust that the developer knows enough to solve the problem" - Jay Shirley, A case for Catalyst.

        I agree. If it's a straightforward task as it usually is, efficiency shouldn't be a concern. Efficiency is therefore only relevant in the rare cases where there are very demanding needs. That's when you need to worry about your algorithms.

        Rob Pike knows his classics:

        Show me your flowchart and conceal your tables, and I shall continue to be mystified. Show me your tables, and I won't usually need your flowchart; it'll be obvious. -- Fred Brooks, The Mythical Man-Month

        He wrote that in 1975. It's still just as true today.

        • another intruder with the mooring in the heart of the Perl

Re: Efficient programming
by eye (Chaplain) on Dec 22, 2008 at 07:49 UTC
    The section on "Efficiency" in Chapter 24 of Programming Perl (3rd ed) has some terse suggestions on how to achieve various types of efficiency.
Re: Efficient programming
by jplindstrom (Monsignor) on Dec 22, 2008 at 13:44 UTC
    If you start with correct, well named and well structured, you may have the opportunity to make it fast and efficient.

    The opposite is rarely true.


    • Write a test - to make it correct
    • Write some code - to make it work at all :)
    • Refactor - to make it well structured
    • Profile and Optimize - every once in a while to make it efficient


Re: Efficient programming
by nagalenoj (Friar) on Dec 22, 2008 at 09:48 UTC
Re: Efficient programming
by dragonchild (Archbishop) on Dec 22, 2008 at 21:52 UTC
    To hop on board what everyone else is saying, evaluate in the following order:
    1. Does it work? If not, make it work first.
    2. Does it run fast enough? If so, stop. Fast enough is determined by need. If it's a batch job, then 8 hours may be fast enough. On the web, anything under 3-5 seconds is fast enough. If you need faster than 0.1s, you're probably using the wrong tools (i.e., Perl isn't the right language).
    3. If it needs to be faster, then find a better algorithm. Look at CPAN.
    4. If the right algorithms is in use, find a better implementation. Look at CPAN.
    5. If all else fails, ask here.
    For the record, this is the exact set of steps I use and have used for over 7 years.

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
Re: Efficient programming
by hangon (Deacon) on Dec 22, 2008 at 19:06 UTC

    The key is to start out with a clear understanding of what you are trying to do. Otherwise you may end up writing a lot of unnecessary code and having to try to optimize it away later.

    What works for me (YMMV) is to start with a pencil & notepad to sketch out the specs, data structures, api and jot down any other relevant information, then outline and refine the program in pseudocode. From there I'll code any algorithms and complex logic independently to work them out. Only then will I start coding the main program.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://731974]
Approved by ikegami
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2018-03-21 09:31 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (265 votes). Check out past polls.