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

  • Customize Your Own Audio Test Cues in Visual Studio 2019 v16.9 Preview 3

    Yes, developers can be alerted to a failed test with a fart sound.

  • Progress Touts New Third-Party Blazor UI Components

    Third-party dev tool specialist Progress announced an update to its .NET-centric offerings, touting new controls for Blazor, Microsoft's red-hot project for creating web apps with C#.

  • Entity Framework Core 6: What Developers Want

    Microsoft outlined its plan for Entity Framework Core 6, which in November will take its place as the data access component of the landmark .NET 6, a long-term support (LTS) release that will mark Microsoft's transition from the Windows-only .NET Framework to an open source, cross-platform umbrella offering of all things .NET.

  • AWS Open Sources .NET Porting Assistant GUI

    After previously open sourcing components of its Porting Assistant for .NET, Amazon Web Services open sourced the tool's GUI.

Upcoming Events