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.

  • 3
    \$\begingroup\$ 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. \$\endgroup\$ – Tetrad May 25 '11 at 13:59
  • \$\begingroup\$ Also, how is this question different from gamedev.stackexchange.com/questions/853/… ? \$\endgroup\$ – Tetrad May 25 '11 at 14:00
  • \$\begingroup\$ @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. \$\endgroup\$ – void May 25 '11 at 14:04
  • \$\begingroup\$ Sigh. Judging by this question, you should be worrying about correctness right now, and performance much later. \$\endgroup\$ – Olhovsky May 25 '11 at 15:15
  • \$\begingroup\$ 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. \$\endgroup\$ – Ali1S232 May 25 '11 at 15:23

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

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.

  • \$\begingroup\$ You cannot recommend them? Or you cannot recommend them too much? (these have opposite meanings) \$\endgroup\$ – user253751 Dec 27 '14 at 11:04
  • \$\begingroup\$ @immibis Wow, Yeah can not recommend them enough.. My bad on that! \$\endgroup\$ – James Dec 28 '14 at 17:28

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.

  • \$\begingroup\$ 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. \$\endgroup\$ – Ali1S232 May 25 '11 at 13:31
  • \$\begingroup\$ 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. \$\endgroup\$ – user1430 May 25 '11 at 15:15
  • \$\begingroup\$ @Gajet You would find loop issues like that by profiling. And why would you loop the wrong way anyway? \$\endgroup\$ – 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.


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.

  • \$\begingroup\$ 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. \$\endgroup\$ – Ali1S232 May 25 '11 at 18:18
  • \$\begingroup\$ 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 :) \$\endgroup\$ – Olhovsky May 25 '11 at 18:20

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