Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

There are many optimization tricks that you can apply to your C++ code that can help speed up your program, and there are more game-related tweaks poping out every day. For example, I've just heard where you are trying to draw some meshes with same texture it's faster to draw them all in one call (or something like that which mean to pass texture once and for all of them).

Are there any books or weblogs or something which demonstrate these things? I'm working with C++ if there are any general advises about optimization, I'll be glad to hear about them.

share|improve this question
2  
This question is too broad. If you find a bottleneck and want advice on how to fix it, feel free to bring it here. Mostly the answers would involve learning the language inside and out, reading up on what the compiler does, and using critical thinking and objective testing. –  Tetrad May 25 '11 at 13:59
    
Also, how is this question different from gamedev.stackexchange.com/questions/853/… ? –  Tetrad May 25 '11 at 14:00
    
@Tetrad So true. There is also a lot of difference between architectures on what optimizations are good. What's good for an x86 will certainly not hold for a Cell, so a good knowledge of your platform is needed. –  void May 25 '11 at 14:04
    
Sigh. Judging by this question, you should be worrying about correctness right now, and performance much later. –  Olhovsky May 25 '11 at 15:15
    
i'm just looking for some guide to do so, and @Tetrad it's almost the same tanx for guiding me to that question. but i'm looking for some tough books that have many optimization tricks for, not just 1 or 2 general advises. –  Ali.S May 25 '11 at 15:23

4 Answers 4

up vote 2 down vote accepted

For general C++ optimization considerations I can not recommend the Effective C++ and More Effective C++ books.

I will also add in that I have contacted this author a few times and he has not only been very forth coming with additional clarifications if need be, but genuine interest in the information.

share|improve this answer

general advises about optimization

  1. The most important thing when optimizing is: Profiling. Learn your code from the hard profiling-facts. Don't make any assumptions about your code, profile it!

  2. Make automated profiling runs and compare the results in a machine readable format, make them easy accessable in graphical performance charts, so you will more likely know which checkin caused new hitches, or whether optimizations were good or not.

share|improve this answer
    
but that's how to find performance bottle necks, i wanted to know how to solve them, eg. when you are working with a two dimentional array just swaping order of the loops may increase your program speed dramatically. –  Ali.S May 25 '11 at 13:31
    
You solve the bottlenecks in a fashion that is appropriate to the bottleneck, there are very few global solutions. You're asking for information that won't help you. –  Josh Petrie May 25 '11 at 15:15
    
@Gajet You would find loop issues like that by profiling. And why would you loop the wrong way anyway? –  The Communist Duck May 25 '11 at 17:22

Situational awareness.

Measure your product, both by manual on-demand profiling with external profiling tools when you see something that acts more slowly than you'd like, any by instrumenting your code with automatic metrics gathering so that you can have some kind of measurements even when your code is running unattended by a developer -- these latter metrics can also be used to create a history of the execution profile of your application and thus provide hints into what changed to make things drop in performance.

There are very few "general solutions" to performance problems other than this (situational awareness). Things that are oft hailed as such can just as often have exactly no effect, or worse, harm either the performance of the application or the quality of the code. Especially in languages like C++, it's really not possible to reason in general about how the shape of a bit of C++ code will translate to the machine instructions actually executed. Too much of the language is undefined and too much is up to the compilers which are usually much better than humans at balancing and pipelining a lot of subtle, multi-factored variables.

Situational awareness is just as important when applying the handful of general advice that is out there -- consider, for example, your note about swapping array indexing order that you mention in a comment to Maik's answer. Do you know why this is (sometimes) true? Do you know what will make it not true, or have no impact? If you can't explain it it can be a dangerous thing to go and try to do. Don't trust, know.

Furthermore, many of these so-called general performance techniques apply in very small and localized contexts and can usually be used for little more than providing performance band-aids rather than full-fledged solutions to performance problems (which may be algorithmic in nature). The way to write high-performance code is to understand the scope and shape of the problem your code must solve and the context-specific tweaks and enhancements you can make. Looking for band-aids won't really help you in the long run, it will just give you a false sense of accomplishment.

share|improve this answer
    
Good answer, +1. –  Olhovsky May 25 '11 at 16:53

Gajet, you are asking us to tell you about all of the millions of possible cases where modifying your C++ code can yield a performance improvement.

A performance improvement at the low level you're describing on one type of hardware is not necessarily a performance improvement on another type of hardware.

For example, your C++ code running on a processor with a 12MB L1 cache might benefit from using a method that consumes a great deal of memory, where that same method yields slower code on a processor with a 512kb cache.

Judging by your question, low level optimizations like this should be the least of your priorities.

If you want to write fast code, first consider learning about different algorithms.

Here's a book on the subject: http://mitpress.mit.edu/algorithms/

The algorithm you choose will often affect your runtime speed much more than any low level optimizations of particular operations in your C++ code.

My program written in Python might run 100x slower than your program written in C++ for small amounts of input data. However, if I choose a better algorithm than you (e.g. heap sort), then my Python code will always run much faster than your C++ code (e.g. insertion sort), no matter how well you optimize it, when the input data size is large.

If you are just getting started with programming (i.e. you have less than 1000 hours of experience) then by far your priority right now should just be building things that actually work, efficiently or not. Otherwise, either start reading about alrgorithms, or profile your code and come here with a specific question about how to make a particular solution go faster on some particular hardware. We can't address all of the millions of possible cases for you here in advance.

share|improve this answer
    
this answer was the closest to my question (at least you refered to a book which could help me optimize my code), i just wanted to know if there are any books which explain some low-level optimizations. –  Ali.S May 25 '11 at 18:18
    
Gajet, I'm glad you found my answer helpful. The type of optimizations you'll find in the book might be reffered to as high-level optimizations though, not low-level :) –  Olhovsky May 25 '11 at 18:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.