Hi,
I caught myself confused about if register keyword should be used in code, having compilers of present days or no? Is it like "use in some rare cases, when absolutely sure" and even then its most likely useless?
I never use it, myself, as the compiler does a good job of doing that kind of optimization on its own.
Honestly I think it's just a hold-over from when compilers weren't as smart, and when computers weren't as fast. I don't really think it has much of a place in modern day programming.
So yeah. I'd say it's useless. Some might disagree though.
I fall into the category of "Some might disagree".
When data is stored in a register then it should be able to be accessed a great deal quicker than in RAM.
If for example, your processor has a speed of 2GHz & your RAM a speed of 600MHz, then the data from RAM can only be processed at 600MHz, if it is stored in the processor in a register then it should be able to be processed at the 2GHz. I might be wrong though, but I believe that is correct...
Yes but compilers optimize to keep frequently used variables in registers automatically without the need of the register keyword. And for larger functions with many variables, it probably isn't benefitial to keep one single variable in a register the whole time (especially if subroutines are called).
Variables aren't written back to RAM every time they're changed. Not unless they're declared as 'volatile' (which is exactly what that keyword is for)
The microsoft compiler completely ignores the register keyword, beyond checking for syntax errors. The days when humans could out-optimize compilers at this level are long past. You have 3 levels of memory caches, complex instruction pipelining, and tons of hidden complexity.
Gone are the simple days when division was slow, and lookup tables were fast, and humans were better at the "M to N register problem" than compilers.