Next Previous Contents

15. Use register variables with care

Register variables may give faster and shorter code, but they do also have an overhead. Register variables are actually zero page locations, so using them saves roughly one cycle per access. Since the old values have to be saved and restored, there is an overhead of about 70 cycles per 2 byte variable. It is easy to see, that - apart from the additional code that is needed to save and restore the values - you need to make heavy use of a variable to justify the overhead.

An exception are pointers, especially char pointers. The optimizer has code to detect and transform the most common pointer operations if the pointer variable is a register variable. Declaring heavily used character pointers as register may give significant gains in speed and size.

And remember: Register variables must be enabled with -Or.


Next Previous Contents