A peer of mine is working on a report that displays the weekly (Sunday to Saturday) advance of every employee in our small consultancy firm. There's a piece of code he wrote that shows the columns corresponding to the days in the target week. His algorithm is the following:
Of course, the flag indicates what the current week is.
I suggested another algorithm:
The "difficult" part in my algorithm is part 1. I mean "difficult" as in "difficult to understand", because the algorithmic complexity of doing it is constant. And my algorithm has the advantage of having tighter loop. My peer's loop does a comparison for every day of the month. Mine doesn't.
This was a little example and you might say that over-optimizing here is a bit too paranoid. But his programming style doesn't change a bit when we write actual performance-critical code.
His code is also full of these tests:
/* doSomething() doesn't change the state of the relevant variables. */
if (condition)
{
flag++;
if (flag > test)
doSomething();
}
else
if (flag >= test)
doSomething();
When, of course, it can be done like this:
if (flag >= test);
doSomething();
if (condition)
flag++;
What do I do?!?!?!
EDIT: I corrected the comparisons in the code samples.
Is this your task, or his? If its his, let him do it. I am a person who thrives on efficiency, in fact I become very frustrated when something seems inefficient and is out of my control.
There are well over 200 people on SO who could put my most 'efficient' ideas, algorithms and code to shame. Probably more, you can't go by rep alone. If Linus Torvalds himself signed up, he'd start at 1 like the rest of us.
What you have to consider is that people need to be able to maintain the code that they write. This means, they have to understand it as if they gave birth to it. Even if someone demonstrated another algorithm as much more efficient than my own, I would not use it unless I was comfortable with it.
If this is a joint project, write it your way, demonstrate the speed up and then spend some very, very patient hours with your peer to help him really grasp it.
Look back on stuff you wrote 5 years ago, everyone has to learn by doing and everyone does stuff at their own speed, especially learning.