0

I would like to start learning and understanding what and when to optimize in a code meant for real time simulations (especially for games). There are various code snippets out there, most comparing standard trig or sqrt functions to their optimized versions. Some require assembly, others rely on architecture specific strategies and some utilize pure mathematical tricks (such as look up tables or approximation functions). Each version claims to be faster, at a minor precission cost.

To my knowledge, there's no compiled material (e.g. a book) explaining what can be done, including the basics (not anyone knows assembly, SSE, SIMD, MMX, FPU, etc.). There are, however, a plethora of books aimed at how to implement some numerical methods (such books are good, but they're not stressing the importance of fast tid-bits because the huge majority of those books do not even include architecture specific code or floating point discussions from a developer's point of view).

So, could anyone please share a very short list of books or other resources that best fit to these scenarios?

P.S.: I've the impression that "Numerical recipes in C++.." or "C++ for Scientists and Engineers.." or "C++ for Scientific Computing.." do not contain a lot (if any) information on this issue.

teodron
  • 1,410
  • 1
  • 20
  • 41
  • 3
    A general advice for optimization is: Don't do it! At least not until you truly need to. Measure, profile and test before you do any kind of optimization. Optimization often make code unreadable and hard to maintain, and so should be the last thing you do, after your project is done. – Some programmer dude Nov 23 '12 at 10:09
  • Yes, that's an advice I fully acknowledge, but there are situations when the naive implementations seem to be more than 2X slower than the optimized code. That's what I want to understand, as a core concern of my question. – teodron Nov 23 '12 at 10:12
  • 3
    Use a profiler when you find you have code thats too slow, I found that by working in a job where speed was critical, with people who had 'done the time' and got the experience was the main way that you can gain the knowledge. I never really found any good books - Google has stuff but it takes a lot of effort to compile and read it.... I know this sounds weak, but thats why its not an "answer" – Caribou Nov 23 '12 at 10:12
  • Is your timing really that tight that a speed up of 2 will be helpful? Real time programming is not so much about being fastest, but about organizing your resources (including time). A speed up of 2 might help today, but if you are that tight, then a small increase in your input data size will kill it again. – Zane Nov 23 '12 at 10:20

1 Answers1

2

A "simulator" is typically a complicated program doing some structured analysis of preset initial data, whose performance depends more on the choice of algorithm and problem parameterization than on the speed of sqrt. Readers of a numerical methods or scientific computing book are more interested in converging faster than square-rooting faster. And sacrificing precision would usually represent a dangerous gamble.

Games usually need to project a set of data representing dynamic game state from one frame onto the next. There's no notion of convergence, "closed-form" equations are applied to advance the state as needed.

Use a game programming book, not a scientific computing book, as a guide. Both will reference "numerical recipes," but games will generally use less.

Don't optimize until there's already a performance problem. Small functions can always be made faster and less precise after you find there's a performance issue. A profiler can identify performance issues easily but nothing will automatically find accuracy issues! Using a library shouldn't prevent you from applying micro-optimizations once hotspots are identified.

I've been using the C++ Eigen algebra library and found it incredibly friendly and fast for geometry. I'm doing science but it's perfectly suitable for games as well. Everything is a flat array that you can manipulate with C or C++ (or assembly, but Real Programmers just make the compiler output the assembly they want) when the built-in operators are unsatisfactory.

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421