Mail Archives: djgpp/1996/12/16/20:45:18
Matt J Reiferson writes:
>I want to have a couple of global register int variables in my program to
>get the most speed out of a couple of functions, when I compile I get an
>error message saying:
> register name not specified for 'int sliver_column'
>
>The line it refers to is:
> register int sliver_column
I very much doubt that global variables can be allocated to registers.
Think what this would mean: no code, anywhere in your program, could use
these registers! Because of the way C compiles each source files as an
individual unit, there is no way that this could be enforced.
You shouldn't need to explicitly declare them as register variables,
though. The gnu optimiser is quite smart enough to temporarily move
variables into registers as required (eg. when they are being used
inside a tight loop), and then to later swap them out in favour of
something else. It is far better to leave this sort of decision to the
compiler: it's more portable, and the compiler has a lot more control
over exactly where things go that you would with a simple 'register'
definition.
If you are really concerned about efficiency, though, you shouldn't make
the variables global. Think about it: every time you call an external
function, the compiler has no way of knowing if the variable has been
changed. For example, if foo is a global int, the code:
for (i=0; i<foo; i++)
do_something();
will have to examine the global var in memory each time around the loop,
becuase do_something() might have changed the value of foo. If you make
foo a local variable, though, the optimiser knows that it's can't be
changed by anything except the loop instruction, so it will load it into
a register for you.
Trust the optimiser - it does a very good job :-)
/*
* Shawn Hargreaves - shawn AT talula DOT demon DOT co DOT uk - http://www.talula.demon.co.uk/
* Ghoti: 'gh' as in 'enough', 'o' as in 'women', and 'ti' as in 'nation'.
*/
- Raw text -