Robot Dreams

Robot Dreams

Monday, January 16. 2006

Algorithmic Optimization

The latest Game Developer touches on a subject that I get asked about frequently. The when, how, and why of optimizing programs. In the Mature Optimization article Mick West discusses the very common mistake programmers make of not considering optimization until the very last step of programming, if at all. This stemming from a nugget of wisdom often quoted and misquoted:

"Premature optimization is the root of all evil." [Hoare] [Knuth]

I think Mick makes a good point about the need to do careful optimizations and not put oneself in the trap of not doing optimizations until it's basically too late. But I think he misses on a more general, and more useful, optimization guideline Algorithmic Optimization. Like many programmers I worry about the usual inefficiencies in programs, some of which Mick mentions, like repeated memory allocations, hidden bottlenecks, bad in-lining, aliasing, RTTI, etc. But the optimization I think about most is the complexity of algorithms I'm implementing and of the complexity of libraries I'm using. Mick lists some optimizations that are obviously in the algorithm category: "AVOID PER FRAME CODE", and "AVOID ITERATIONS" which are more classically know as: use amortization algorithms, and use data structures to reduce complexity.

To help me in understanding the complexity of code I use a simple documentation trick. For each non-trivially complex function I write, I also add a comment indicating what I understand the complexity to be. This includes accounting for any interior calls made. For example one of the big operations in Thot is doing differences, the base algorithm of which is suffix arrays:

// O(E+6N+2NlogN), or O(E+5N+NlogN) without LCP sorting
void SuffixArray::build()
{
    // O(3N)+O(E)
    histogramSort();
    // O(NlogN+N)
    arraySort();
    // O(N)
    calculateLCP();
}
// O(3N)+O(E)
void SuffixArray::histogramSort();
// O(NlogN+N)
void SuffixArray::arraySort();
// O(N)
void SuffixArray::calculateLCP();

Which gives a clear idea of where one might optimize to get significant improvements. You might ask why I bother with constants. Simple; they may be constant but knowing that a function is 10N, and possibly in need of optimization, is a much better state than just saying it's N and having no idea why it's slow.

In the near future I'll talk about some specific algorithm optimizations that I've used in the past. And also on how to spot the hidden complexity in the plethora of Computer Science papers that claim incredible performance. Both John and I have some serious horror stories on that subject :-)

Posted by Rene Rivera in Programming at 23:44 Comments (0) Trackbacks (0)
Defined tags for this entry: complexity, gamedev, optimization, programming

Trackbacks
Trackback specific URI for this entry

No Trackbacks

Comments
Display comments as (Linear | Threaded)

No comments

Add Comment

Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.
Standard emoticons like :-) and ;-) are converted to images.
You can use [geshi lang=lang_name [,ln={y|n}]][/lang] tags to embed source code snippets
E-Mail addresses will not be displayed and will only be used for E-Mail notifications

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA 1CAPTCHA 2CAPTCHA 3CAPTCHA 4CAPTCHA 5


 
Submitted comments will be subject to moderation before being displayed.
 

Calendar

Back September '10
Mon Tue Wed Thu Fri Sat Sun
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30      

Archives

September 2010
August 2010
July 2010
Recent...
Older...

Categories

XML Diversions
XML Life
XML Meta
XML News
XML Politics
XML Programming



All categories

Show tagged entries

aspenxml
autostitchxml
boostxml
c++xml
cameraxml
chicagoxml
conferencexml
doorxml
drinkingxml
evanstonxml
hardwarexml
hawaiixml
kauaixml
liberalxml
panoramaxml
photoxml
planexml
politicsxml
programmingxml
robotxml
torontoxml
travelxml
weatherxml

Quicksearch

Syndicate This Blog

XML RSS 1.0 feed
XML RSS 2.0 feed
ATOM/XML ATOM 1.0 feed

Other

  • Blogrolls...
  • Blogroll Me!
  • Subscribe with Bloglines