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

  • Full Stack Hands-On Development with .NET

    In the fast-paced realm of modern software development, proficiency across a full stack of technologies is not just beneficial, it's essential. Microsoft has an entire stack of open source development components in its .NET platform (formerly known as .NET Core) that can be used to build an end-to-end set of applications.

  • .NET-Centric Uno Platform Debuts 'Single Project' for 9 Targets

    "We've reduced the complexity of project files and eliminated the need for explicit NuGet package references, separate project libraries, or 'shared' projects."

  • Creating Reactive Applications in .NET

    In modern applications, data is being retrieved in asynchronous, real-time streams, as traditional pull requests where the clients asks for data from the server are becoming a thing of the past.

  • 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.

Subscribe on YouTube