Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Types of Efficiency

by David Caughell (Monk)
on Feb 03, 2004 at 22:48 UTC ( [id://326342]=perlmeditation: print w/replies, xml ) Need Help??

I've seen all kinds of code on perlmonks, and it appears to me that there are definitely different types of efficiency, when it comes to evaluating whether or not code is "good".

To put it another way, I guess you could say that when it comes to efficiency: TMTOWTDI

Though this topic has probably been discussed, I supersearched titles and didn't find that it was discussed in its own node (search:efficiency importan).

I'm going to throw out a few ideas, and anyone who wants to add to this is encouraged. This is my amateur opinion, so if some professionals want to give their views, that would be great.


Obviously, how long it takes a program to compile and execute (more importantly, to execute), is a good judgement of efficiency, in many situations.

In other situations, your efficiency could be measured by how little memory is used, or how little storage is used, or how easily your peers understand your code, (they might not be perlmonks). Another measurement of efficiency is how long it takes you to write the code (trading machine time for programmer time).

In node 326181 I wrote a script which when running a 733 or 750 AMD chip, took only a second or so to compile and execute. It produced output to a text file, within an accuracy that was customizeable within the program.

I believe that in this case, the "efficiency" of this program is better judged as the speed of transfering the output (i.e. bandwidth) or how long it would take to find information in it using an editor / browser. This is mostly because it uses a small amount of processing power and because it only needs to be run once.

(That being said, I titled the node "Percentage to Fractions", so it was good of them to post some efficient code in there, in case someone else looks for the same thing!)

An argument in favour of making any script most efficient in the traditional sense is that one day the programmer will more than likely need to write something that runs fast, so they might as well learn how to as soon as possible.

Maybe someone ought to make a node where people can post little tricks to make their programs compile faster.

Replies are listed 'Best First'.
Re: Types of Efficiency
by atcroft (Abbot) on Feb 04, 2004 at 01:24 UTC

    Efficiency can be thought of in a lot of ways. In my college CS classes, we were introduced to "big-O" notation as one way to compare the efficiency of algorithms based on how the number of the most costly operation involved changed with the number of elements to consider. In my daily work, efficiency is more likely to be defined in terms of acquiring the required results in the way that causes the least amount of stress to systems (be they in terms of processing, memory, network usage, etc.). As to your comment regarding the apparent efficiency of something by the amount of time before it was returned to the browser (a specific case), try unbuffering your output ($| = 1;)-I have seen the effect of that particular one before, in the case of a script that took several minutes to return all of the data it was returning. By unbuffering the output of the script, portions of the data were made available much quicker, which occupied the user, making it seem much quicker (although the script's execution time did not change).

    There are a number of resources you can consult for a more formal study of efficiency in general. As far as perl efficiency tricks, I believe there are some listed near the end of Programming Perl, as well as some of the other good perl books that are available. (I thought there was also something in some of the perl documentation itself, near a section for people coming from other languages, but I think I am confusing sources.)

    Hope that helps.

    Updated: 04 Feb 2004: The unbuffering of output is $|=1;, and not $! as I typed earlier. Corrected above. My appologies for that typo, and my thanks to davido for the catch.

Re: Types of Efficiency
by exussum0 (Vicar) on Feb 04, 2004 at 12:27 UTC
    In other situations, your efficiency could be measured by how little memory is used, or how little storage is used, or how easily your peers understand your code, (they might not be perlmonks). Another measurement of efficiency is how long it takes you to write the code (trading machine time for programmer time).

    ...

    I believe that in this case, the "efficiency" of this program is better judged as the speed of transfering the output (i.e. bandwidth) or how long it would take to find information in it using an editor / browser. This is mostly because it uses a small amount of processing power and because it only needs to be run once.

    You so beat around the bush with this statement. efficiency only comes down to one general thing: resources.

    Which ones you have, don't have, need back and want back. Your script runs in 30 seconds, 15 seconds to compile and 15 seconds to execute. Is it efficient? What are your time constraints, your memory constraints? What about disk storage constraints? No use of saying your program is great if it can't fit on your hard drive. Also in writing output. If your program uses data files that get too big, too fast, well, it's useless. A final example is your screen. I've had programs that won't allow for resize with ncurses beyond 80x25. Efficient use of screen? Nope. I just "wasted" a large chunk of screen when I didn't want to.

    I think I've driven the point home. Efficiency is all about your needs and wants in measurement of resouces. So don't limit yourself one way, because some day, you may hurt yourself trying to make things great in one direction, but not in the others.


    Play that funky music white boy..
Re: Types of Efficiency
by Sol-Invictus (Scribe) on Feb 04, 2004 at 08:47 UTC
    Mastering Algorithms With Perl covers efficiency theory and O notation fairly concisely among other things. The Benchmark module is another way to optimise code along with profiling (perl5 -d:DProf test.pl).

    Of course if you're referring to when some one tags their node or reply with "I know this code is messy/not optimised etc." it's more to do with the fear of being criticised I believe :)

Re: Types of Efficiency
by flyingmoose (Priest) on Feb 04, 2004 at 14:40 UTC
    Efficiency is about making proper tradeoffs between *multiple* resources.

    Example -- when memory is at a surplus, trading memory for increased speed, knowing when to trade RAM usage for disk usage, when to walk a file iteratively versus loading it entirely into memory, and so on. Also, this includes knowing when to use proper resources for the proper problems.

    Classic bad examples in the monastery involve building giant hashes to do complex searches when SQL would have been the better choice (for CPU, for speed, for RAM... in fact, all of the above...).

    A lesser known form of efficiency is maintaince efficiency. That is, is your code clean and to the point, and how well can it be tweaked to accept new backends, etc. Though only tangentially related, efficiently written and organized code is easier to optimize and will tend to be more efficient when actually running.

Re: Types of Efficiency
by Courage (Parson) on Feb 04, 2004 at 10:38 UTC
    Obvious method to increase efficiency is using Inline::C and optimize critical places.

    But mostly efficiency could be increased individually for a given problem, with such a methods as improving algorithms, using hashes, and so on...

    Courage, the Cowardly Dog

Re: Types of Efficiency
by grendelkhan (Sexton) on Feb 05, 2004 at 03:14 UTC

    I'm surprised no one here's quoted... well, I don't know which guru said it, but it sure as heck wasn't me. The idea is that a single-use program that takes ten minutes to write and one second to execute is less 'efficient' in the global sense than one that takes one minute to write and ten seconds to execute.

    Remember Mark Jason Dominus's #11908: "Premature optimization is the root of all evil."

    To a large extent, worrying about efficiency when you're not doing a pimp-load of IO, or scads of numerical computation, is wasted effort. Factor in the time you, the coder, spend making the program, and you'll get a much better idea of the efficiency. Consider the global system.

Re: Types of Efficiency
by DaWolf (Curate) on Feb 06, 2004 at 14:40 UTC
    Hi there, great node ++

    Just my 2 cents (sorry if someone already said it, I'm in my lunch break and couldn't resist to post here):

    Efficiency (in the extended sense of a "good program") isn't just about time. I've learned in my Logics 101 (best course I've ever tooked) that a good program must comply with these points:
    • Efficiency
    • Stability
    • Security*
    • Clarity**
    • Speed
    • Documentation
    And the most important thing:
    • The program must do what it's suppose to (note that this is not the same as efficiency)
    * Specially important in the "internet daze"
    ** By this I've meant that the code must be clear, understandable, sorry if the english term isn't correct, I'm a foreigner (brazilian =:c)

    Best regards,

    my ($author_nickname, $author_email) = ("DaWolf","erabbott\@terra.com.br") if ($author_name eq "Er Galvão Abbott");

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://326342]
Approved by Roger
Front-paged by Courage
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-03-29 07:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found