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.

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.
If this question can be reworded to fit the rules in the help center, please edit the question.

3

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

@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.
–
voidMay 25 '11 at 14:04

Sigh. Judging by this question, you should be worrying about correctness right now, and performance much later.
–
OlhovskyMay 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.SMay 25 '11 at 15:23

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.

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.

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.SMay 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 :)
–
OlhovskyMay 25 '11 at 18:20

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.

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!

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.

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.SMay 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 DuckMay 25 '11 at 17:22