DevDisasters

Modular Process Improvement

After nearly eight years of working as a C++ developer at Rik V.'s insurance company, a certain coworker was finally laid off.

One task that fell to Rik was to run an application that his former colleague wrote, which compared two directories and put any files that had changed into a third. This was a semi-frequent task, and one that his coworker had devoted quite a bit of time to each month.

Performance Issues
The first time Rik ran the application, he noticed that it was taking an exorbitant amount of time to complete. After five minutes, it barely scratched the surface of the directories, so Rik took off for lunch and returned later to see that the app had run for 43 minutes. Curious as to how a directory comparison could take so long, he peeked at the code. Here's what he found:

long ReadBinaryFile(CString 
		strFile, BYTE** pResult)
{
    BYTE* pBuffer[256];
    BYTE* pResultBuffer = NULL;
    long nLenResultBuffer = 0;
    CFile file;
      
    if(!file.Open(strFile, 
	 CFile::modeRead)) {
      return NULL;
    }
      
    UINT nBytesRead = 256;
    while(nBytesRead)
    {
        nBytesRead = 
		file.Read(pBuffer, 255);
        if(nBytesRead)
        {
            BYTE* pNewBuffer = 
			new BYTE[nBytesRead 
		 + nLenResultBuffer];
            ZeroMemory(pNewBuffer,
 			nBytesRead 
	  + nLenResultBuffer);
            memcpy(pNewBuffer, 
			pResultBuffer, 
   nLenResultBuffer);
      memcpy(pNewBuffer 
		 + nLenResultBuffer, 
 pBuffer, nBytesRead);
      delete[] pResultBuffer;
      pResultBuffer = pNewBuffer;
      nLenResultBuffer 
	    += nBytesRead;
    }
 }
 *pResult = pResultBuffer;
 return nLenResultBuffer;
}

"I was stunned," Rik said, as he relayed the situation to Doug, who was in the next cubicle. "For a 5MB file, his code would require 20,000-plus loops, 20,000-plus memory allocations of gradually increasing size, and at least 40,000 memcopy operations. And that's just for one file!"

Do-Over
Not knowing whether to laugh or cry, Rik decided to take the safe approach and just rewrite it.

After rewriting, he restarted the app-and it finished in less than 20 seconds. With a couple of logic tweaks elsewhere in the code, he got this down to 13 seconds-from 40 minutes.

"I'm not sure if the guy was a moron or a genius," Rik told Doug. On one hand, the code was absolutely appalling. On the other, it gave the former coworker the opportunity to say: "Sorry, the application is still running; I've got to wait for it to finish"-and then go back to reading the newspaper.

About the Author

Alex Papadimoulis lives in Berea, Ohio. The principal member of Inedo, LLC, he uses his 10 years of IT experience to bring custom software solutions to small- and mid-sized businesses and to help other software development organizations utilize best practices in their products. On the Internet, Alex can usually be found answering questions in various newsgroups and posting some rather interesting real-life examples of how not to program on his Web site TheDailyWTF.com. You can contact Alex directly via email at [email protected].,

comments powered by Disqus

Featured

  • Lessons Learned Building a GenAI-Powered App

    Sometimes, complex technical achievements are best explained through one example. That's the approach Mete Atamel, Developer Advocate at Google, is taking as he makes the rounds detailing the capabilities of Vertex AI and associated tooling on the Google Cloud Platform.

  • 30th Annual Visual Studio Magazine Reader's Choice Awards Announced

    For the 30th year in a row, Visual Studio Magazine readers have chosen the best tools and services for developers. The 2024 winners are honored in 43 categories, from component suites to testing tools to AI helpers.

  • Another Report Weighs In on GitHub Copilot Dev Productivity: 👎

    Several reports have answered "yes" to the question of whether GitHub Copilot improves developer productivity. A new one says "no."

  • Logistic Regression with Batch SGD Training and Weight Decay Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end program that explains how to perform binary classification (predicting a variable with two possible discrete values) using logistic regression, where the prediction model is trained using batch stochastic gradient descent with weight decay.

Subscribe on YouTube