Av rating:
Total votes: 7
Total comments: 1


Chris Massey
Mysteries of the NET Framework: The Challenge
22 September 2008

Mysteries of the .NET Framework

Andrew Hunter has been described as the master of all that is deep and dark in the .NET Framework. In order to write the guts of ANTS 4, he needed to be very familiar with aspects of the framework that the ordinary application programmer wouldn't need to understand. However, there is one point at which you and Andrew would be interested in the same thing: Tuning applications to make them perform well.

The more you look at .NET performance, the more you hedge your opinion with words like 'it depends…'. We therefore thought that it would be interesting to focus a discussion on four different .NET performance questions. To make it more interesting, we gave away prizes for the best contributions.

Andrew selected the four questions that deserved interesting answers. These were:

  1. When processing some data into a list, is it faster to build up a collection and return it as an enumerator, or use yield to create the enumerator 'on the fly'?
  2. What is the best approach to doing fast complex string manipulation in .NET?
  3. What are the differences between a debug and a release build in C#? How do they affect performance?
  4. When is it a good idea to start using more than one thread?

To make it more interesting, we decided to award a free license to ANTS 4 to the best entries. Andrew Hunter judged the answers, assisted by the usual Simple-Talk distinguished panel of judges, including Phil Factor, Andrew Clarke, Tony Davis and James Moore. You'll see, if you read the responses, that the questions really struck a chord with our readers.

The winners were, as usual, very close. After a considerable amount of deliberation, Andrew Hunter and the panel came up with the following winners, in order of merit

  1. Eric Gunn on yield. A well-deserved win
  2. TheCPUWizard  on strings
  3. Steve S on threads
  4. Nuno Gomes for his answer on processing a list
  5. Lewis Moten for spotting some catches with the list question
  6. Hugh Worm's analogy deserves something
  7. ovalsquare for his observations on debugging
  8. dmajkic for his effort (though there usually isn't a need to use unmanaged code)

Andrew Hunter writes:

"There was a gotcha in the yield question, which does tie in to the debug builds question as well. If you time code using yield in a debug build you'll generally find that it runs much slower than it does in a release build, leading to the impression that using an array is faster: this illustrates that it's not just important to measure the performance of your application, but to make sure that you're measuring it using the right builds."

"The debug question essentially had two ways of answering it: from the point of view of what the compiler does differently, or what the run-time itself does differently. The C# compiler will optimise the code when asked to produce a release build, which reduces the size of the resulting file and can result in a performance increase. I say 'can' increase performance here because the JIT can legitimately apply the same optimisations, resulting in different IL but the same ultimate performance. Debug builds also have some extra attributes applied to them by the compiler that instructs the JIT to generate code that is easier for a debugger to work with: this is the DebuggableAttribute attribute."

"One thing we were trying for with the strings question was to illustrate that the right solution for one situation is not necessarily universal. For instance, StringBuilder works very well when building lots of very small strings, but starts to perform poorly as the string size increases. Internally, it stores the string as an array of bytes, so as it grows it has to spend more and more time copying the string around. Eventually the string size will exceed the largest available fragment in the large object heap and it will fail with an OutOfMemoryException: due to the way the heap works, this is usually much smaller than the maximum memory available to the application. Setting the capacity in advance or building the string in fragments is a good way to work around this: TheCPUWizard's suggestion was a good way to deal with this issue."
 



This article has been viewed 4476 times.
Chris Massey

Author profile: Chris Massey

Chris has recently come from a background in HR appraisal tools to write and edit for Simple-Talk. When not doing either of those, he's either getting an education in SQL Server & .NET, or debating philosophy and breakdancing.

Search for other articles by Chris Massey

Rate this article:   Avg rating: from a total of 7 votes.


Poor

OK

Good

Great

Must read
 
Have Your Say
Do you have an opinion on this article? Then add your comment below:
You must be logged in to post to this forum

Click here to log in.


Subject: Now if only ...
Posted by: Charles Kincaid (not signed in)
Posted on: Wednesday, October 15, 2008 at 10:03 AM
Message: ... you would do this same sort of thing for those of us doing Mobile applications. (sigh)

 






recommended site pinvoke

PInvoke.net is a user-driven wiki which provides .NET developers with native method signatures, so they don't have to spend time writing them from scratch.





Damon Armstrong
Customizing the Login Page in SharePoint 2007
 Damon shows how a few simple steps lead you to being able to include the login form in a consistent look and feel to...  Read more...


ANTS Profiler and the Un-Rest Cure
 After a while, successful applications can get set in their ways. Bart Read and Andrew Hunter decided... Read more...

Silverlight-Speed Loop
 John Bower steps up a gear, produces a Lamborghini, and examines the process of using a high-speed... Read more...

Sid: Vicious
 Dan Archer documents his epic struggle with an apparently simple task of authenticating user... Read more...

Embedding Help so it will be used
 It is not good enough to make assumptions about the way that users go about getting help when they use... Read more...

Optimising a High-Performance Computing Tool
 Many computer systems nowadays have their ‘correctness’ checked using sample testing, but this isn't... Read more...

A Complete URL Rewriting Solution for ASP.NET 2.0
 Ever wondered whether it's possible to create neater URLS, free of bulky Query String parameters?... Read more...

.NET Application Architecture: the Data Access Layer
 Find out how to design a robust data access layer for your .NET applications. Read more...

Web Parts in ASP.NET 2.0
 Most Web Parts implementations allow users to create a single portal page where they can personalize... Read more...

Visual Studio Setup - projects and custom actions
 This article describes the kinds of custom actions that can be used in your Visual Studio setup project. Read more...

Beginning ASP.NET 2.0
 It seems that there is both excitement and confusion surrounding Master Pages and Themes. A big part of... Read more...

Over 150,000 Microsoft professionals subscribe to the Simple-Talk technical journal. Join today, it's fast, simple, free and secure.

Join Simple Talk