Perl Optimization

How can I make my perl program run faster?

  1. Benchmark and profile to make sure that you are optimizing the right part.
  2. Look for better algorithm instead of microtuning your code, and when all else fail, consider buying better/faster hardware.
  3. Autoload seldom-used Perl code. See AutoSplit and AutoLoader module.
  4. Locate bottlenecks and think about writing those parts in C or assembly. See Inline module.
  5. If your perl executable is currently linked to libc.so, you can often gain 10-25% performance benefit by rebuilding it to link to the static libc.a instead. This make a bigger executable but your perl program may thank you for it. If your server is only for serving mod_perl code, you may want to double check.
  6. Using substr() or vec() to simulate arrays can be highly beneficial.
  7. The standard Tie::SubstrHash module can also help for certain types of data structure.
  8. If you're working with specialist data structures (matrices, for instance) modules that implement these in C may use less memory than equivalent in Perl modules.
  9. Another thing to try is to learn whether your Perl was compiled with the system's malloc or with Perl's builtin malloc. Whichever one it is, try using the other one and see whether this make a difference. You can find out whether you are using perl's malloc by typeing "perl -V:usemymalloc"
  10. Don't read an entire file into memory if you can process it line by line.
  11. Avoid using map and grep on large list.
  12. Avoid unnecessary quotes and stringifications
  13. Avoid stringifying arrays: { local $, = ""; print @big_arrays; }
  14. Pass by reference. Pass arrays and hashes by reference, not by value. It avoids creating a copy of all the contents.
  15. Tie large variables to disk. For big data stores, ones that exceed available memory, consider using one of the DB modules to store it on disk instead of RAM. This will incur a penalty in access time, but that's probably better than causing your hard disk to thrash due to massive swapping.
  16. Don't use English.pm because it export a lot of variables (need verification)

A Timely Start

page_revision: 1, last_edited: 1224784976|%e %b %Y, %H:%M %Z (%O ago)
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License