Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: speed question about C programs

Author: David Blackman

Date: 03:27:54 11/28/99

Go up one level in this thread


On November 27, 1999 at 04:16:53, blass uri wrote:

>Does someone know about a book about the fastest C program to do simple tasks?
>(for example to find if a number is a prime)
>
>I think that knowing this information can help to think about ideas how to do
>chess programs faster.
>
>I understand that +- operations are faster than */ operations but I do not know
>how much faster and if it is dependent on the computer.
>
>Where can I get information like this information?
>I can find it by doing a simple program but I prefer not to check everything
>about speed by testing because I may have many question like this question.
>
>Uri

I the good old days when microprocessors were simple, you could just look at the
clock cycle counts for all the instructions in the manufacturers handbooks and
that would tell you, at least if you were writing assembler. (Unless you used
the 8088 where the handbooks were mostly wrong on cycle counts :-)

Today it is just too complicated and if you really care you just have to test it
and see. A few obvious things though:

Conditional branches are often slow. They can be fast if the hardware can always
predicts correctly if it branches or not. But if the prediction is wrong, it
will be slow.

Multiply is getting quite quick on most CPUs now, but is still slightly slower
than add or subtract. Be happy about using it if it really does something
useful. But divide is really slow.

Random access to main memory is really, really, incredibly slow. Sequential
access is quicker, but still a bit of a worry.

Random access to second level cache is fairly slow.

First level cache is pretty quick, but not as quick as registers.

Don't bother to second guess the compiler on register allocation. Most of them
do pretty well. But remember there is a finite supply of registers (especially
on X86) and don't use excessive numbers of local variables and complex
expressions unless they actually do something useful.

Register to register operations that go really fast include move, add, subtract,
bitwise and/or/xor/not, compare (but if you do a compare, you will probably
follow up with a conditional branch which might be slow). Shift is often a shade
slower, but still faster than multiply, and perhaps faster than first level
cache access.

Function calls vary a lot in speed. If you call through a function pointer (or
do a C++ virtual method call) expect it to be slow. Otherwise it could be as
quick as maybe ten fast instructions, if there are not too many parameters and
local variables to copy. Probably you should explicitly tell the compiler which
functions to inline, because if the compiler inlines everything in sight the
code could get big and you could get more misses in the instruction cache.

All this will vary a bit from compiler to compiler and chip to chip. So don't
take it too seriously.

And look at using good algorithms, then determine which functions in the program
are taking most of the time, before you worry about this kind of thing at all.



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.