A commute can be as sweet as a
rare, impeccable gift of relentless time or it can be a burgeoning burden of
boredom. Either way, one thing that isn’t debatable about commutes is the fact
that they consume time regularly and hence significantly.
A commute as we all know, is
routine trip between the place of work and home. For the sake of this post I am
only concerned about the routine bit in the above sentence. A commute is a
routine trip between two places. Routines take a toll if you don’t or can’t
embrace their merits. One place where this applies is in a computer program. A
computer program must be fast and not just that, it must be as fastest as it
could be because otherwise it isn’t considered effective let alone impressive.
Just like in a commute, a
computer program also sometimes requires regular trips between processor and
memory. Of course memory is where the variables are sitting and processor is
where these variables take part in the actual computations. Consider the
following loop:
int i;
for( i = 0; i <
SOME_BIG_NUMBER; i++)
{
print
i;
}
As an exercise, calculate the
number of times the variable i’s value is needed to be fetched from the memory
to the CPU and then all the way back to memory again. Once you are done with
that, try and fathom number of such back and forth itineraries to i if the
number of iterations are invincibly large. As I said before, routines take a toll
and in this case, the routine memory access consumes time (when time is often
the key focus for parsimony by the programmers). This is a travesty you may
think, but it’s not. There is though, some noticeable room for improvement.
What is the most obvious solution
to the problem of a long commute? Lessen the distance between the two
terminals. Advertising (yet again) my adamant admiration for analogies, I would
like to introduce a similar concept that ameliorates that above loop crisis of
sorts.
Since reading the value of a
variable from memory for each iteration is the main resource consuming
exercise, why don’t we somehow bring the variable to the CPU’s vicinity? And you bet we do. Remember, you might have
studied that the CPU also contains its fair share of registers? Well in case
one of these lesser known registers are available, we can always request the compiler
to let our variable rent the register for a program run. This request is issued
using the “register” keyword in C/C++. All you need to do is add
this keyword as a prefix to the variable declaration like so:
register int i;
That’s it, your part is done. Now
it is up to the compiler to see if such arrangements can be made. After all the
CPU registers are kind of prestigious and hence are seldom free. If our
register request fails, then the variable will reside among other common folks
in RAM.
One of the benefits of using
register is of course making access to most frequently used variables as fast
as possible. But there is one added advantage to this as well. If someone knows
how to use register variables the way they are meant to be used, then reading that
person’s code becomes a little easier. Because then if anywhere in the program
you a see a register variable, you can be sure to note that particular variable
to be heavily used in the remainder of the program. So register not only speeds
up variable access, it also points out popular variables. All in all, a good
trick of the technical trade.
No comments:
Post a Comment