52 Tips for .NET Performance Analysis

52 Tips for .NET Performance Analysis

How can I make my application faster? Many of my customers ask me this question when they invite me to perform a performance analysis of their .NET application. Of course, there is no one-size-fits-all answer to this question. As a rule, the only thing that helps is targeted profiling of the application. Nevertheless, there are some best practices that, if followed, can lead to enormous improvements in application performance. 52 Tips and Tricks to boost NET Performance Book Cover

The Red-Gate team has compiled 52 such performance tips. Performance experts from around the world were asked to contribute their best tips. The result is a short e-book, to which I was also able to contribute two tips.

The book is divided into the following sections:

  • General performance advice
  • .NET performance improvements
  • ASP.NET performance tips
  • Tips for data access
  • Memory optimization
  • General tips

My tips relate to data access with Entity Framework. One problem I often see is that data is unnecessarily loaded with change tracking enabled. Change tracking is a mechanism that ensures that Entity Framework is informed of any changes to an entity’s data. This way, a simple call to the SaveChanges method on DBContext is sufficient to automatically save all changes later on.

However, it is often clear when loading that the data will not be changed. If you use the standard Entity Framework 6 method, as shown in the following code, the data is automatically retrieved with change tracking enabled.

using (var db = new NorthwindDb())
{
    var shippers = db.Shippers.ToList();
    foreach (var shipper in shippers)
    {
        Console.WriteLine("{0} - {1}", shipper.ShipperID, shipper.CompanyName);
    }
}

The solution to this problem is relatively simple. Calling the extension method AsNoTracking() disables change tracking for entities loaded in this way.

using (var db = new NorthwindDb())
{
    var shippers = db.Shippers.AsNoTracking()ToList();
    foreach (var shipper in shippers)
    {
        Console.WriteLine("{0} - {1}", shipper.ShipperID, shipper.CompanyName);
    }
}

The question now is, of course: What does calling AsNoTracking() achieve?

In my example, in which I read 10,091 records from the Customers table of the Northwind database, I measured the following values:

With change tracking: 10,958 ms

Without change tracking: 9,047 ms

As we can see, using AsNoTracking saves just under 2 seconds in this case.

Of course, that’s still not particularly fast. That’s why I’ve published another tip in the e-book that addresses another common problem with data access using Entity Framework. If you want to know what it is, take a look at the e-book :-)

By the way, I recorded a short video some time ago in which I explain the general procedure for performance profiling with the ANTS Performance Profiler. So if you’re unsure why your application is running too slowly, you should take a look here.

Do you have any great performance tips? Then write them in a comment on this post!