Alex Rivera | Logout

How can I speed up my Perl program?

Asked 2008-10-07T03:23:24.687
31

This is really two questions, but they are so similar, and to keep it simple, I figured I'd just roll them together:

  • Firstly: Given an established Perl project, what are some decent ways to speed it up beyond just plain in-code optimization?

  • Secondly: When writing a program from scratch in Perl, what are some good ways to greatly improve performance?

For the first question, imagine you are handed a decently written project and you need to improve performance, but you can't seem to get much of a gain through refactoring/optimization. What would you do to speed it up in this case short of rewriting it in something like C?

Please stay away from general optimization techniques unless they are Perl specific.

I asked this about Python earlier, and I figured it might be good to do it for other languages (I'm especially curious if there are corollaries to psycho and pyrex for Perl).

Edit
Report

2 Answers

32

There are many things that you might improve on, so you first have to figure out what's slow. Others have already answered that question. I talk about this a bit in Mastering Perl too.

An incomplete list of things to think about as you are writing new code:

  • Profile with something like Devel::NYTProf to see where you are spending most of your time in the code. Sometimes that's surprising and easy to fix. Mastering Perl has a lot of advice about that.

  • Perl has to compile the source every time and compilation can be slow. It has to find all the files and so on. See, for instance, "A Timely Start", by Jean-Louis Leroy, where he speeds everything up just by optimizing module locations in @INC. If your start-up costs are expensive and unavoidable, you might also look at persistent perls, like pperl, mod_perl, and so on.

  • Look at some of the modules you use. Do they have long chains of dependencies just to do simple things? Sure, we don't like re-invention, but if the wheel you want to put on your car also comes with three boats, five goats, and a cheeseburger, maybe you want to build your own wheel (or find a different one).

  • Method calls can be expensive. In the Perl::Critic test suite, for instance, its calls to isa slows things down. It's not something that you can really avoid in all cases, but it is something to keep in mind. Someone had a great quote that went something like "No one minds giving up a factor of 2; it's when you have ten people doing it that it's bad." :) Perl v5.22 has some performance improvements for this.

  • If you're calling the same expensive methods over

answered 2008-10-07T08:44:49.373
1

If your code needs speeding up then chances are that your test suite does too. This talk touches on the key points:

Turbo Charged Test Suites

answered 2008-10-10T12:46:16.877

Your Answer