[ale] gcc
Danny Cox
danscox at mindspring.com
Wed Apr 3 07:01:32 EST 2002
On Tue, 2002-04-02 at 23:02, Jeff Hubbs wrote:
> Oh, yeah! The expression "2*a" is less cycle-friendly than "a+a".
> There would probably be a break-even point beyond which that doesn't
> help ("a+a+a+a+a+a"?) but you can experiment to find out.
>
> Stay away from doing things like (pseudocode follows)
>
> for t=1 to 10 do
> if x < 3*t then
> do_stuff;
>
> Why? The 3*t gets calculated every time. Better to do
>
> for t=3 to 30 step 3 do
> if x < t then
> do_stuff;
>
>
> You might be able to do things like create lookup tables for things that
> would otherwise be calculated. Instead of
>
> for a = 1 to 3 do
> for b = 1 to 4 do
> munge(a*b);
>
> you might go
>
> g = [1 2 3
> 2 4 6
> 3 6 9
> 4 8 12];
> for a = 1 to 3 do
> for b = 1 to 4 do
> munge(g[a, b]);
Okay, hold it right there, buckaroo! While all these ideas are good,
most programmers get caught in the trap of early optimization, usually
at the expense of readability. In your example above:
for t=1 to 10 do
if x < 3*t then
do_stuff;
in my opinion should remain as it is. Why? Because that's probably NOT
the bottleneck in the program! Code for readability first (if you've
got to maintain it, you'll save yourself grief later, and if it's
someone else, they'll thank you), then use a profiler to see where the
code is using most of it's time *if you need to*! If it's fast enough
already, leave well enough alone!
I don't know about this specific example, but gcc has plenty of smarts
in optimizing code. Common sub-expressions, loop-invariants, and simple
things like multiplying/dividing by a constant power of 2 are all taken
care of.
Speaking of that, the higher optimizations in gcc (-O[239]) usually
result in faster but larger code. It'll unroll loops before you can
say, "What the heck?". Yes, I've seen some package, I *think* it was
cdparanoia that used "-O9". I shudder to think what all gcc did to that
poor code. It works, though.
Okay, I'm off the soapbox, now. Sorry, but I've had to fix too many
sections of code where the original author was coding for "efficiency".
Whose efficiency? Certainly not mine!
--
kernel, n.: A part of an operating system that preserves the
medieval traditions of sorcery and black art.
Danny
---
This message has been sent through the ALE general discussion list.
See http://www.ale.org/mailing-lists.shtml for more info. Problems should be
sent to listmaster at ale dot org.
More information about the Ale
mailing list