Ah, Perl. Often derided as a "write-only language", Perl nevertheless enjoys some level of popularity among WTF-savvy workplaces. So when David inherited a codebase through the purchase of a company staffed entirely with, what he had dubbed "mentally challenged chimpanzees", it seemed only natural that the code was entirely in Perl.
Dealing with financial transactions is one of the key features required for any online shopping or banking applications, and as such, it's more or less a solved problem by now. So of course, the perl gurus at ChimpanCorp reinvented the wheel with their Real-time Financial Processing app. It would calculate the user's balance every ten minutes by compiling all data posted since the last calculation, leading to a balance that was never more than ten minutes out of date! Of course, that's as good as you can possibly get when the calculation takes nine minutes to run. Still, the client was wondering if maybe the could speed up the calculations so it could run, say, every five minutes? Or maybe, hypothetically speaking, immediately after every transaction?
David dug into the codebase, clocking various methods -- it was best to limit his exposure to the actual code as much as possible, given the warning labels attached to the documentation. Something about eye strain and fecal matter... In any event, he finally pinpointed one method that was taking seven minutes to run. If he could shave off a few minutes from that, they might be able to hit the mythical five-minute benchmark.
The code, of course, had to run twenty queries on un-indexed SQL; adding a few indexes removed two minutes from the runtime, which unfortunately wasn't quite enough. So he dug into the queries themselves, carefully commenting as he went, saving a few seconds here and there with better optimized joins. And then he came to this gem:
my $mysql = 'SELECT TOP (1) n = (ROW_NUMBER() OVER (ORDER BY number))-1 FROM [master]..spt_values ORDER BY n';
my $mysth = $dbh->prepare($sql);
$mysth->execute(1, 10);
while (my @row = $sth->fetchrow_array) {
$OurZero= $row[0];
}
David laughed, replacing this with a simple "$OurZero = 0", and moved on. One query down, only nineteen more to optimize. Four hours later, he reached the bottom of the method. This was it, what the entire seven-minute runtime was calculating:
return ($ReturnValue * $OurZero);
David stared at the return value, then up at the method he'd been labouring to understand all day. Seven. Minutes. With a final sigh, he went to the calling function and replaced the execution with this super-optimized bit of perl:
if (1 == 2)
{
#Only run this if you've lost your mind
CalculateZero();
}
Just as he checked his revision into the repository, a coworker popped his head into David's cube. "David, you will not BELIEVE how they're calculating dates!" she gushed.
"Somehow, I suspect I will."