Home > ASP.NET, Performance > Performance Programming with ASP.NET C#

Performance Programming with ASP.NET C#

It is easy to develop using ASP.NET, you can have a web application with database operations up in less than half an hour. But this ease of development comes at the expense of several performance hits.

  1. Viewstate
    Web sites are stateless by design, which means that anything you have on the page is not retained when you submit the data. What ASP.NET tries to do is to save the state on the page so that state is retained when the page submits its data.

    However this quickly becomes unmanageable when you have too many controls on the page, especially if you have large server controls e.g gridviews. As viewstate is stored on the page itself, a simple page with a few form elements may end up sending large amounts of data on postback, the majority of which is the viewstate itself.

    Other than increasing page size, there is also the performance cost incurred when serializing and deserializing the viewstate.


    • if a page is an output page and does not do any further processing, you can disable the viewstate for the page <% Page EnableViewState=”false”.
    • If the control on the page does not handle any events, is not a data-bound control or the data i constantly refreshed on postback, then you can disable the viewstate for the control
    • If all else fails, you can look into storing viewstate on the session state using SessionPageStatePersister (There is a drawback because the session state may expire before the user is done with what he or she is doing on the page – think blogs, wikis etc)
  2. String concatenation & comparisonHaving experience with int i = 0; i = 1 +2, you decide to go ahead and do the same for strings. string s = “aa” + “bb”This is very bad programing, simply because strings work differently from numeric primitive data types. When you do this type of string concatenation, quite a fair bit of resources are created to hold the strings and results

    The same can be said for case insensitive string comparison, usually you would do something like string1.toLower() == string2.toLower(). Same thing occurs, temporary space needs to be created to store the lowered strings and then do a compare.


    use stringbuilder for string concatenation functions
    System.Text.StringBuilder sb = new System.Text.StringBuilder();

    use the overloaded string compare function if you want to do case insensitive searches
    String.Compare (string strA, string strB, bool ignoreCase);

  3. For loops with easily checked conditions perform far better than for each loops. However if the for loop condition requires a property call (e.g list.count()) then it will perform much slower, in this case get the count before the loop and assign it to a variable and in the for loop check against this variable.
  4. If you have multiple exit conditions, put them when it is first possible to check and also put the one most likely to happen right on top. This reduces the amount of checks needed to exit, which can give you a performance improvement.
Categories: ASP.NET, Performance
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: