SkepticScott
TAM Volunteer Guy
In C, "register" is only a hint to the compiler:What gives you faster performance than const is declaring them register.
"May be used for local variables or parameter declarations. It is equivalent to auto, except that it provides a hint to the compiler that the object will be heavily used and should be allocated in a way that minimizes access time."
-- C: A Reference Manual, fifth edition, page 83
The only way to be sure is to time the code, alter it, measure it again, try another change, etc.
This leads to another comment I wrote, against a block of assembly-language that ORed two buffers together to make a third. I forget the exact wording, but it was something along the lines of
; OR together graphics[] and text[], and then write to ShiftOut, which sends the data to the printhead.
; This looks like it could be done faster, but the obvious alternative
; (there was an example block of assembly language code here)
; runs faster than the prefetch queue can run, so the 3-clock instructions will take
; 4 clocks, and therefore run slower than the code below, which takes X clocks per 16 bits
; N.B. ShiftOut takes 17 clocks to shift out all the bits and to return to the Ready state, so don't write to it faster than that
That product had a hardware interface that would write commands from the host device directly into the product's RAM. No protocol, no ISR, just a bus mastering for a couple clocks, so the software would poll the values and look for changes. The person who wrote it commented all those values with
// automagically changed by HW when the host writes to us
both when they were declared, and every time they were used. Otherwise a new person reading the code would see something like
Code:
if( NewModeFlag )
{
SetNewMode();
NewModeFlag = 0;
}
Code:
// NewModeFlag is automagically changed by HW when the host writes to us
if( NewModeFlag )
As others have written, be sure to update the comments when you change the code. People can see what the code is doing by reading the code, but the comments have to explain why. I've got a button that reads "Code as if whoever maintains your code is a violent psychopath who knows where you live". And sometimes said "violent psychopath" is you, because products often have longer lives than we anticipate, and you may have to maintain your own code a few years after you wrote it. The better your comments, the faster you can make the change and get back to the Shiny New Project that you're working on.