Tuesday, April 05, 2005

Efficiency maniac

I could be such an efficeincy maniac at times!

Valuable lesson for someone who's teaching/guiding
people in programming. Don't stress too much on
efficiency. So much so, it blinds people into a frenzy
where they can't see correctness before efficiency.
What's the use of a highly efficient code that does not
do what is expected?!

Look at this girl... I give her an assignment to write
some code that would poll for a signal on a bus and notify
the client. There already was some code in place that was
doing the polling stuff - only thing she had to do was to
write the partthat notifies the client. The only grave
mistake that I did was to ask her to keep an eye on
efficiency! Damn me!

// code that was doing the polling...
extern volatile UInt8 *isrSigTable;
// ...

UInt8 sigMask;
for(UInt8 step = 0;
    step <= MAX_INTERVAL;
    step += pollInterval
   )
{
  sigMask = reinterpret_cast< SigTable >(isrSigTable[0x10]).mask;
  // do other necessary stuff...
}
As per my instructions, she gave more than an eye
towards this piece of code and changed it to...

// code that was doing the polling...
extern volatile UInt8 *isrSigTable;
// ...

UInt8 sigMask = reinterpret_cast< SigTable >(isrSigTable[0x10]).mask;
for(UInt8 step = 0;
    step <= MAX_INTERVAL;
    step += pollInterval
   )
{
  // do other necessary stuff...
}

No PhD required to say that the code won't work as
expected. I must hand it to her for recognising what
might look like a loop invariant. But in an embedded
interrupt driven environment and when the variable
is declared volatile, shewould have been better off
without taking pains to optimise it. You tell the compiler
- "Don't you look at that for anyo f your darn optimisations",
and you go and do it yourself?!

"Just a suggestion - listen to some heavy music while
coding. Research has it that it enhances awareness and
concentration. I've got a CD if you want...". I told her this
jokingly - did not want to tell all that's said above to her.
She's still a student - I was the same a year ago. But it
feels good to be guiding students. I've always liked it. :-)

No comments:

Post a Comment

What I want to say is: