This will be a thread about optimization and efficiency concerns in both programming in general, and specifically to perl, if need be. Making algorithms more efficient is a much needed ability to any programmer, and there are some common tips/tricks/whatever that can help.
Personally, I know of a few, and wondered what else was out there--perl-related or not. Some of what I'll offer is this:
(1)The unrolling the loop technique (not Friedl's regex optimization). To explain, the first code example is more than likely slower than the second:
for($i = 0; $i <= 10_000; $i++) {
$ary[$i] = init_val($i);
}
$idx = 0;
for($i = 0; $i < 10_000; $i += 10) {
$ary[$idx++] = init_val($idx);
$ary[$idx++] = init_val($idx);
$ary[$idx++] = init_val($idx);
...rest goes here...
}
(2)Shifting for Multiplying and Dividing. It should come naturally that moving over some bits by a few positions is relatively fast for a computer based entirely on bits, and doing mathematical operations such as multiplying are about an order of a magnitude faster. For example:
$x = $y * 16;
$x = $y << 4; #Much faster
To get this to work for something besides a power of two what we do is use the fact that $y * 26 can be expressed as this:
$y * 26
== $y * 16 + $y * 8 + $y * 2
== $y << 4 + $y << 3 + $y << 1;
Also, division is fater to be expressed not as $x / 6 but as $x * 1/6, which holds true for most general cases.
(3)Look-up tables can make for faster code
(4)sin(angle) = cos(angle-90) and cos(angle) = sin(angle+90)
(this helps, I should note, in forming look-up tables of sine or cosine values; also, tan is redundant, so using just sin or just cos can make it faster)
(5)x % y can be expressed better as x & (y-1) (*big note* this is only for powers of 2, e.g. x%64==x&63
(6)Assembly is faster generally, if not always--if implemented correctly; though I'm not aware of how to get inline assembly code in perl, it's easy in C/C++ with _asm.
Update: To the rather bold question of whether I need to optimize: yes. Well, at least in my current project; I'm working on making a game, and parts of the game need to go real fast to make it look realistic. The game will be done in C++, and that's why I focussed the post on 'C-style constructs' and things that may not work too well in an interpreter language. I just posted on this site because (1)there isn't a C site with as good feedback, and (2)some things are general, and many perl programmers came, or have experience in, C or C++. I should have said something to avert this confusion, so I apologize; but I hope that explains myself.
I also fixed the for loop, and added some things that I should have put in the original post.
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.