gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Beginners > Faster C/C++ faq?

#11260 - yaustar - Tue Sep 30, 2003 2:46 pm

Is there an FAQ somewhere that lists the operations that shouldn't be used in c/c++ for speed?
_________________
[Blog] [Portfolio]

#11264 - poslundc - Tue Sep 30, 2003 3:12 pm

The biggest things to avoid are division and modulus. Bitshift and mask where you can; multiply by the reciprocal where you can't. If timing is critical and you must divide/modulus, use the SWI dispatch in the bios (documented in various FAQs floating around) instead of gcc's regular divide. Consider using a look-up table if your value is in a known range.

Trigonometric functions obviously are terribly expensive to compute. Use look-up tables instead. The GBA even has a couple built in that you can copy out, if you can get to them (I personally don't bother to, since you end up consuming the memory anyway, but there is information on how to do so floating around).

Don't use floats or doubles, period. These have to be emulated in software and are slow as all heck. Learn how to use fixed-point mathematics. It's easier than it looks, and there's tons of info on the net on how to do it.

Calling functions creates extra overhead, so be smart about it. Small functions that get called repeatedly can often be replaced with macros.

These are the main things I would say to avoid. There's plenty more you can optimize in your code, but premature optimization is never a good idea. Get it working first, then go from there.

Dan.

#11265 - yaustar - Tue Sep 30, 2003 3:22 pm

I have heard things about avoiding virtual functions, does this diminish the use of classes?
_________________
[Blog] [Portfolio]

#11268 - tepples - Tue Sep 30, 2003 4:13 pm

poslundc wrote:
The biggest things to avoid

Thanks for the tips. I've added explanations of some of these techniques to the Beginners' FAQ.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#11371 - poslundc - Fri Oct 03, 2003 5:25 pm

tepples wrote:
Thanks for the tips. I've added explanations of some of these techniques to the Beginners' FAQ.


Cool, I've been FAQ'd. Today I am a man.

Dan.