[LCP]register question

Erik Mouw J.A.K.Mouw at its.tudelft.nl
Wed Nov 21 21:45:10 UTC 2001


On Mon, Nov 19, 2001 at 11:21:48AM +0100, Joachim Bauernberger wrote:
> I have referred to some textbooks but all it says is that:
>  
> "You can declare register int in order to speed up some cpu intensive code. 
> You should only declare them register inside a function (i.e. not global 
> variables). Some of the register types will be used and some not depending 
> how many registers are available in the CPU at the time..."
> 
> My question. When do I know that using register will gain me some processing 
> speed. (Apart from when I am actually running the program ant checking with 
> time) 

Sorry, but the golden rule of optimisation always applies: use a
profiler before you do any optimisation at all.

> Is there really any benefit from declaring an int with register on modern 
> Pentium class CPU's? 

No.

To wake you up from a pretty dream: Pentium class CPUs (even the P-4)
are not at all considered modern. It's a design that dates back from
the early eighties (8086), and it has legacy things like segmentation,
a real mode, and an overly extensive instruction set. Even worse is
that it has only four general purpose registers (eax - edx), of which
three are also used for things like indexing and shift counts, which
leaves you with only one real general purpose register. Compare that
with other CPUs which usually have a RISC architecture with plenty of
general purpose registers (at least 16).

> Or is the compiler already smart enough to know when he should internally 
> speed things up so it wouldn't really be necesarry with gcc?

Yes. Modern compilers are smart enough to put the correct variable in a
register.

> Is there any drawbacks from using register (apart that i can not use &var) 
> I.e if I use register somewhere where there would be no gain then this 
> variable will nevertheless take up space in CPU?

The drawback is that you can get less optimal code because you force
the compiler to put the wrong variable in a register: in order to
enforce that, it has to do excessive loads and stores for the other
variables.


Erik

-- 
J.A.K. (Erik) Mouw, Information and Communication Theory Group, Faculty
of Information Technology and Systems, Delft University of Technology,
PO BOX 5031, 2600 GA Delft, The Netherlands  Phone: +31-15-2783635
Fax: +31-15-2781843  Email: J.A.K.Mouw at its.tudelft.nl
WWW: http://www-ict.its.tudelft.nl/~erik/



More information about the linuxCprogramming mailing list