Optimizing Perl - Some Tips
Perl is fast, but not that fast...
Still need to take care with apparently simple things in 'hot' code
- Function/method calls have significant overheads per call.
- Copying data also isn't cheap, especially long strings (allocate and copy)
- Perl compiles to 'op codes' then executes them in a loop...
- The more ops, the slower the code (all else being roughly equal).
- Try to do more with fewer ops. Especially if you can move loops into ops.
Key techniques include:
- Caching at many levels, from common sub-expression elimination to web caching
- Functional programming: @result = map { … } grep { … } @data;
But don't get carried away... only optimize hot code, and only if needed
- Don't optimize for performance at the cost of maintenance. Learn perl idioms.
- Beware "Compulsive Tuning Disorder" - Gaja Krishna Vaidyanatha
- And remember that "Premature optimization is the root of all evil" - Donald Knuth