starX has asked for the wisdom of the Perl Monks concerning the following question:

I beseech the wisdom of the monastery in helping me figure out how to make certain parts of a program run more quickly. I started rethinking my general approach after reading [an older node on the subject].

Improving the speed of the application in general isn't necessary, but in the cases of a few specific subroutines, it would be highly beneficial. I have a couple called as command line arguments that simply print data to the screen and exit, and they take almost as long to do that as the application does to execute.

Can anyone suggest a way to optimize my application for some specific subs? Many thanks.

Replies are listed 'Best First'.
Re: Selective Optimization
by derby (Abbot) on Dec 18, 2007 at 20:37 UTC
    1. Find out where it's slow -- search for 'profiling'
    2. Find out why it's slow -- post snippet or search for idiomatic perl
    3. Compare alternatives -- Benchmark is your friend
    4. ???
    5. Profit

    (I just had to throw those last two in there!)

    -derby
Re: Selective Optimization
by moritz (Cardinal) on Dec 18, 2007 at 20:51 UTC
    If the slowness is mainly due to I/O slowness, there's not really much that you can do.

    You can try to use buffered IO handles, and you can produce less output (for example when you write HTML pages, don't encode every char > 127 as an HTML entity - send utf-8 (and proper headers) instead).

    But it's hard to tell, if you don't show us the code.

      I suspect that the slowness of the app in general is due to IO (and database connections), it does a little bit of both over the network. For what I'm trying to optimize, however, it does neither.

      I totally hear you on needing to see the code to answer anything specifically about where the problem might be. I'm unfortunately not in a position where I can post it in its entirety. Thanks, though.

        Can you at least explain a little more clearly what the subroutines are doing? "a couple called as command line arguments that simply print data to the screen and exit" doesn't tell me much. How long does it take to run? How much time do you want to trim?

        If what you need to do is speed up specific subroutines, have you looked for any code that can be pulled out of sub? Are you opening/creating/etc some resource that could be held open to remove that step?

        Optimization questions are pretty hard to deal with unless you have at least some idea of the problem.