[ale] gcc

Jeff Hubbs hbbs at attbi.com
Tue Apr 2 23:02:48 EST 2002



Chris Fowler wrote:
NFBBLCEFBCFEPMJJKOIKKEBACFAA.cfowler at outpostsentinel.com">
  Optimization has more to do with coding style.  gcc with the -O<level> willprovide some level of optimizations.  Maybe someone on this list couldpoint to a good document or book that provides excellent infoon good coding practices in C.  I know many of the booksI have skip this important information.  Basically you needto use fewere CPU cyles as ell as other resources.Hopefully you'll never have to use inline assembly inyou C code.  I have not needed it.  Ususally it is neededwith mathamatical programs.  I'm having a hard enoughtime with multiplication.  Stay away from multiplication anddivision in your programs.  Those operations can use toomany cycles.
  
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]);
  
For really complicated logic, you can replace a lot of nested conditionals
with Boolean expressions that you base on a truth table external to the actual
code.  Deriving Boolean expressions from truth tables is called "logic reduction"
and you can usually do it by hand for up to four input variables, six if
you are good at visualizing in three dimensions.  I did this once, back when
I used to code; I had eight expressions, each in eight variables, and the
expressions were determined external to the coding itself via logic reduction.
 The goal in this case was to minimize the size of the source code, which
was written in the all-but-dead interpreted ATLAS language.  
  
And, for small values of t, sin(t) is nearly equal to t
 if t is in radians.  I would imagine that this gets worked into games
all the time.  
    
  
  




More information about the Ale mailing list