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."
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.