Fine tuning C++ code?

Started by
20 comments, last by DevLiquidKnight 20 years ago
I was wondering if anyone knows of a site that talks about some various techinques to fine tune your code, so that it is faster.. ect.. Anything about this area... would be great. If anyone has any links id like to read C++ code in particular [edited by - DevLiquidKnight on April 6, 2004 2:56:12 AM]
Advertisement
step 1: run profiler
step 2: notice which functions take the most time
step 3: optimize
do this until your program is fast enough
Here are some more tips:
If you expect functions to be inlined, you probably(depending on your compiler) need to have them defined in a header file.
Pass large classes by const&
Make sure you turned on optimizations in your compiler
Be aware of cache issues - for instance, don't iterate multi-dimensional arrays in the wrong order
Don't worry about psuedo-optimizations like >> instead of /
Use the appropriate algorithms for the job
Understand that while most algorithms consist of time/memory tradeoffs, on modern processors memory is often more precious than time(due to the huge penalties of cache misses)
Prefer stack allocation(or inline allocation in a heap-allocated class) to dynamic allocation, if possible.
Understand where your cpu time is going.
Check your allocations - make sure you know what's getting allocated when, and make sure that you're expecting it.
Understand that certain simple looking constructs(assigning a vector, or returning one from a function, for instance) can hide a deeper cost(eg, invoking the copy constructor for a very large object).

The first step of perf is measuring. If you don't measure, you're just wasting your time - it's as simple as that.

Second, budget your perf. Have a target in mind, and stop when you reach it. You're trying to make a game, and you need to make an engineering decision as to when perf is good enough.

[edited by - sjelkjd on April 6, 2004 5:37:50 AM]
Learn inline assembly.

Its easy to interface assembly instructions into you C++
source code and for mathematically intensive functions, could
gain you increases in speeds from around 150% to 1000%+ or
more!!!

But then again, you REALLY have to know what you''re doing in
asm (assembly) in order to beat the C++ compiler.

BTW its not really neccessary to fine tune ALL your code. I''d
only fine tune code which is time critical. Sure, code wisely
and efficently, but if it ain''t time critical, don''t lose
sleep over it.

Programming these days is hard enough without having to worry
about EVERY single line of code you write!!!

A decent book to get you started on asm, is ''Assembley Language
Step - By - Step'' by Jeff Duntemann. It really digs deep into
how your PC works on a fundamental level, and explains the ins
and outs of the assembly language itself.

However, its not targeted towards inline asm, which is what you
want, but at least its a start.

I''ve yet to find a decent book on inline asm I''m afraid...

K...Hope this helps!!!
I''d add something else, gleaned from years of experience: Optimize at as high a level as you can. A single high-level optimization can eliminate 50% or more of the operations your program has to do, at one fell swoop. Compare that to something that saves one or two cycles per 500-cycle loop, and you''ll see what I''m getting at.
fractoid''s is some of the most important advice. Learn it well.

Rules of Optimization:
Rule 1: Don''t do it.
Rule 2 (for experts only): Don''t do it yet.
- M.A. Jackson

See Wiki: PrematureOptimization for more.

Also Martin Fowler''s Yet Another Optimization Article
see also

Tuning Performance and Process

and the earlier articles which will deal with the high level design issues fractoid talks about.

Refactoring with Martin Fowler
A Conversation with Martin Fowler, Part I
by Bill Venners
get a tool!

http://www.critticall.com/

Abnormal behaviour of abnormal brain makes me normal...www.zootfly.com
quote:
step 1: run profiler
step 2: notice which functions take the most time
step 3: optimize
do this until your program is fast enough


I''d make this a while instead of a do loop:

while (program is too slow) {
step 1;
step 2;
step 3;
}
I''ll tell you what...

If you REALLY wanna go nuts on optimization techniques, get
Intel''s docs on low-level optimization.

The Intel manual is called:

IA-32 Intel Architecture Optimization

It''s about 600 A4 pages long.... but FREE!!!

Go to Intel''s Developer''s Website at:

http://developer.intel.com/design/pentium4/manuals/index2.htm

They also have various other manuals on offer... And you can
either download them or order them via post... Absolutely FREE!!

And considering that these guys ARE the chip makers, you won''t
get better advice anywhere else!!!

So... go knock yourself out with some untapped Intel power!!!

Peace!!!

This topic is closed to new replies.

Advertisement