in reply to What is the most efficient perl switch/case statement?

So, since there are so many ways to code switch/case constructs in perl (half a dozen in the camel book alone), which way is the most efficient as far as execution speed goes?

It seems to me that you should be more interested in which method is easiest to understand. Programmer time is always worth more than CPU time. I usually use an if/elsif/else ladder since most programmers can figure out what that's doing even if they're weak on perl skills.

But if you're really interested in which is faster, develop a large data set that would cause each branch to succeed individually or all fail, and then apply that data set to a particular switch construct. Mostly though, I think you'll be benchmarking your ability to implement the switch (And the test) rather than the particular switch-style.

Update: Fine. For the pedants out there: Programmer time is almost always worth more than CPU time. Of course there are situations where execution speed is critical. In my experience though, if you're programming in perl, execution speed isn't your top concern. :-)

  • Comment on Re: What is the most efficient perl switch/case statement?

Replies are listed 'Best First'.
Re: What is the most efficient perl switch/case statement?
by Abigail-II (Bishop) on Nov 21, 2003 at 16:20 UTC
    Programmer time is always worth more than CPU time.
    That is a common myth, and understandably quite popular with programmers whose favourite programming language trades speed for programmer ease.

    But in many case, it isn't true.

    • In a fly-by-wire airplane, the flaps better go out right after the captain pulls the handle, delays because the company saved on programming time isn't acceptable.
    • If you are one of a group of websites selling "X", and making your living out of it, it's important to attract enough customers to survive. Customers base their selection of website on several things, and speed plays a rather important role.
    • If you write a chess engine, it better be fast, or else it loses on time.
    • Etc, etc, etc.
    Some things just need to be fast, and the program speed dropping below a certain treshhold is not acceptable. In such cases, CPU time is worth more than programmer time.

    Abigail

      I realize you are making a general point, but it seems unlikely that choosing the slowest switch statement implementation in Perl will cause any aircraft to go down. People learning programming should be aware that micro-optimizations of this kind rarely have any significant payoff, and are typically only worth doing after using a profiler to see where the problems lie.
        I wasn't talking about the specific case of a switch in Perl. But the cliche made said that programmer time was always more important than CPU time. If duff meant a more specific case, he should have been more specific.

        Abigail

        #main system check loop big_airplane_002 while (1) { $status = Poll_status_on_hydraulic_systems($wingnumber); act_on_status($status); }
        What happens if Poll_status_on_hydraulic_systems uses a switch that runs 2x as slow as another viable choice? You lose polls on the equipment. Not saying that this would ever be done in perl (go go ADA) but you get the drift. The statement that programers time is more valuable than CPU is just silly. If you want to rationalize passing the buck by being a lazy programmer go for it. Its not for me.


        -Waswas