delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/05/14/03:49:45

From: Joe Kraska <jkraska1 AT NOSPAM DOT san DOT rr DOT com>
Newsgroups: comp.os.msdos.djgpp,rec.games.programmer
Subject: Re: Any tips on optimizing C code?
Date: Tue, 13 May 1997 19:00:32 -0700
Organization: The Devil Himself
Lines: 39
Message-ID: <33791CC0.7013@NOSPAM.san.rr.com>
References: <33775c59 DOT 19219875 AT news DOT cis DOT yale DOT edu>
NNTP-Posting-Host: dt9h2n24.san.rr.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

> I'm interested in understanding what can be done to speed up straight
> C code. In the specific thing I am writing, I've already done the
> obvious things, like switched most calcs from FP to integer, using bit
> shifting wherever possible for multiplying and dividing, etc. But is
> there a complied source of information on just

The kind of optimizations you have named here are often
better to let the compiler handle, or at least they are
when you consider value versus effort spent (fine tuning
games withstanding, I suppose). gcc, for instance, will
convert a manually implemented dual-mask bit rotate to
an actual rotate instruction on pentium hardware.

What I don't know is your background, and whether or not
this will seem pedantic, but... your best bet for optimization
is likely to attack scaling issues (O()), not trying to shave
time off _for_ loops.

For instance, imagine that in some game you regularly
calculate x=f(y) where y is some floating point number between
0 and 100, and f() is some extrememly expensive non-optimizable
calculation. Are you stuck? No. If you are willing to accept
x for some y of no more than 1/1000th precision, you can 
calculate x for all y between 0 and 100 and store this in
a table before you release your game. You can then write
a look-up function that determines x=f(y) nearly instantly,
as opposed to whatever exteremely intense calculation it
was performing before.

It also helps to know exactly when to use hashing, linked
lists, arrays, b-trees, and so on.

Oh, and another thing about optimization! Don't bother
optimizing a function until your profiler is telling you
that it is one of the places you are spending a great
deal of computation time...


Joe.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019