Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re: Performance improvement in perl scripts

by cavac (Deacon)
on Apr 26, 2012 at 13:15 UTC ( #967345=note: print w/replies, xml ) Need Help??

in reply to Performance improvement in perl scripts

Note: This is my completely personal view. If you don't agree, i fully understand. In my opinion, program optimization is just as much art as it is technical. And art is in the eye of the beholder...

That really, completely depends on the problem your program is trying to solve and the way you implemented it. Not for every problem, "Code optimization" is really the best choice. Personally, the first thing i do is think about the design choices i make/made and try to see if there are better ones.

For example:

  • If you are working with huge amounts of data in text files, you might consider switching to a modern open source database instead. Depending on your requirements (relations/normalized data or simple Key/Value store) you'll have to pick an appropriate database system.
  • If you do lots of complicated math, you might consider switching parts of your program to C/XS
  • If you do rather simple math operations, but the same ones on lots and lots of datasets, you might think about learning CUDA or OpenCL
  • If you do lots of network stuff (especially server side), have a look at the multithreaded and/or preforking alternatives line Net::Server
  • If you to many external program calls with backticks or similar, you have a basic design flaw. Try to find some Perl-internal functions or fitting CPAN modules.
  • If you always call the same external program (and you can't find something that fits on CPAN), look if that programs functions also come as a library. Maybe there's a way to use XS to make a Perl module. This would save a lot of external processes being created/destroyed and would make execution much faster.
  • Take a look at your computers harddisk and/or network LED's. Are they (nearly) constantly on when your program is running? If yes, this is a sure sign that your program is IO bound (e.g. it's performance is limited by IO rather than processing speed). In case of harddisk IO, get faster disks into a Raid10 array and or make a better file format with more compact data representation. If network bound, well get a faster link if possible and try to minimize protocol overhead.

I could probably continue the list for another 50 to 200 items, but you get get the basic idea.

In short: In-depth source code optimization should be one of the last steps. First, check that the basic program design is sound and you are not constrained by external limitations. If everything else checks out, then you go fiddling with algorithms, re-ordering loops and making unreadable code...

"You have reached the Monastery. All our helpdesk monks are busy at the moment. Please press "1" to instantly donate 10 currency units for a good cause or press "2" to hang up. Or you can dial "12" to get connected directly to second level support."
  • Comment on Re: Performance improvement in perl scripts

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://967345]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (2)
As of 2018-05-25 23:07 GMT
Find Nodes?
    Voting Booth?