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

  • AI for GitHub Collaboration? Maybe Not So Much

    No doubt GitHub Copilot has been a boon for developers, but AI might not be the best tool for collaboration, according to developers weighing in on a recent social media post from the GitHub team.

  • Visual Studio 2022 Getting VS Code 'Command Palette' Equivalent

    As any Visual Studio Code user knows, the editor's command palette is a powerful tool for getting things done quickly, without having to navigate through menus and dialogs. Now, we learn how an equivalent is coming for Microsoft's flagship Visual Studio IDE, invoked by the same familiar Ctrl+Shift+P keyboard shortcut.

  • .NET 9 Preview 3: 'I've Been Waiting 9 Years for This API!'

    Microsoft's third preview of .NET 9 sees a lot of minor tweaks and fixes with no earth-shaking new functionality, but little things can be important to individual developers.

  • Data Anomaly Detection Using a Neural Autoencoder with C#

    Dr. James McCaffrey of Microsoft Research tackles the process of examining a set of source data to find data items that are different in some way from the majority of the source items.

  • What's New for Python, Java in Visual Studio Code

    Microsoft announced March 2024 updates to its Python and Java extensions for Visual Studio Code, the open source-based, cross-platform code editor that has repeatedly been named the No. 1 tool in major development surveys.

Subscribe on YouTube