Topic : Performance Programming Applied to C++
Author : Joris Timmermans
Page : << Previous 3  
Go to page :


I'll just list a few of the things you can do, that may seem very obvious, but you shouldn't pass over!

a*b + a*c = a*(b+c);
This gets rid of one multiplication, with no change to the meaning of the expression.
b/a + c/a = (1/a)*(b+c);
A variation on the previous item, but this time, replacing two divisions by a division and a multiplication. On every platform that I am aware of, divisions are slower than multiplications, so this rewrite will provide a speed improvement.
(a || b ) && c ) = c && ( a || b );
Perhaps not so obvious, but the C++ standard requires lazy evaluation. In this case, whenever c happens to be false, in the first case ( a || b ) has to be evaluated, whereas in the second case, it can be skipped as the entire expression can never evaluate to true anymore.
I am sure there are more, you can always suggest these to me, so I can add them to this article.

2. Taking advantage of the architecture.

Say you're working on that nice PentiumIII of yours. You have 32bit registers, but you are performing operations on 16bit numbers. In some cases, depending on the operation and the chance of overflow, you could perform two operations in one register, at once.

This is what SIMD (Single Instruction, Multiple Data - exactly what we were talking about), like SSE, 3DNow! and MMX were designed for - allowing this kind of thing.

I'm by no means an expert, so I can't give you specific examples, but I thought I'd give you a general idea on how you might be able to use it - sometimes even without MMX or SSE.

Part 6 and conclusion: Use that brain of yours

Sometimes, all the code optimization in the world won't make your program run faster. Sometimes, the optimizations lie in a different direction. Take a step back, rethink your algorithm, try to think up ways of doing it differently. Who knows what you might come up with!

As an example, think of sorting algorithms. With all the programming skill in the world, bubble sort will still be slower than quicksort, even though they perform the same function. Sorting is an area where there has been lots of research into making it fast. For your specific program, or algorithm, this is probably not the case. Perhaps you can find that little shift of thought that allows you to beat your previous idea by an order of magnitude.

Another thing is knowing how the library functions (and classes) you use work, exactly. What they do, what they don't do, things they do that you don't need. CString might be a nice class, but if you use it just to pass a filename, you'll be adding overhead you don't need, because you won't be performing any of the special operations on it, nor will you need the special reference counting it uses. New/delete are two more examples. They are generic memory-allocation constructs, and they probably do more than you need. If it's critical, write your own operator new and delete, and you might get a very healthy speed increase for your particular class.

In the end, there are a lot of ways to make code run faster. I've listed some, and I hope they've helped you a little. My references section has some more food for thought, or at least interesting reading. I alluded to it in the introduction as well - the best thing is to know what you are doing, and what you are working with. Read up on the programming language you are using, many other people are likely to have tried writing faster code using that language. Read the documentation on the web that you can find on programming and optimization. Check out manufacturers' webpages, they have some good things there too.

Browse through my references, that's a start, at least ;). References:

Efficient C++: Performance Programming Techniques Dov Bulka and David Mayhew, Addison Wesley, ISBN 0-201-37950-3. This book is about how to get to the bottom of the C++ language, and is a very good read for people using that language for their projects. - Intel Developer Services. Lots of information on how to make your code run faster on Intel processors - The same for AMD processors - Some Pentium Optimization tricks - More Pentium Optimization tricks

The end! Really ;-).

Send any comments or suggestions to Mad Keith ( .

Page : << Previous 3