C# .NET - Solution for poor performance with application in real time

Asked By dessiy martin on 08-Aug-11 01:24 AM

We are coding a trading application and using .net platform and almost completed it. Our problem is its performance, when we use it for trial purpose, its very slow and don't update data in real time when we try to sort and filter the data. Is it possible to get sharp and  quick response, when data is change or update. 

Jitendra Faye replied to dessiy martin on 08-Aug-11 01:26 AM

For a good Application you should concentrate in two fields-

1. DataBase Performance
2. Application Performance

1. DataBase Performance

Here you should follow this-

To increase performance of the query follow these:

1.Create Index in the table.
2.Have a Primary key in the table while creating.
3.Use Joins instead of subqueries.
4.Instead of selcting "*" specify the Column names in the select statement.

2. Application Performance

To increase performance of the Application follow these:

1. Turn off Session State, if not required
2. Disable View State of a Page if possible
3. Set debug=false in web.config
4. Use the String builder to concatenate string
5. Use Finally Method to kill resources
6. Use Client Side Scripts for validations
7. Avoid unnecessary round trips to the server
8. Use Page.ISPostBack
9. Use Foreach loop instead of For loop for String Iteration
10. Use "ArrayLists" in place of arrays

Follow these for your Application.

Hope this will help you.

1. Turn off Session State, if not required 2. Disable View State of a Page if possible 3. Set debug=false in web.config 4. Use the String builder to concatenate string 5. Use Finally Method to kill resources 6. Use Client Side Scripts for validations 7. Avoid unnecessary round trips to the server 8. Use Page.ISPostBack 9. Use Foreach loop instead of For loop for String Iteration 10. Use "ArrayLists" in place of arrays Hope this will help you.
Ravi S replied to dessiy martin on 08-Aug-11 01:27 AM

you  need to do the following inorder to increase performance of an application:-

1. Declare connection string in a centralised way i.e in web.config. 2. Write stored procedures instead of sql statements. 3. Write repeated code in a common function and reuse it. Apart from all these,they are a good no of issues discussed in the links given below.
Go through all these:

Here are links:
1.http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag/html/scalenetchapt06.asp 2.http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/dotnetperftips.asp 3.http://geekswithblogs.net/ranganh/archive/2005/06/02/41661.aspx 4.http://davidhayden.com/blog/dave/archive/2005/07/02/1881.aspx
Riley K replied to dessiy martin on 08-Aug-11 01:36 AM
There are many areas that you need to look into

 First tune your database with proper indexes

 Do not use so many sub queries instead use Joins

Also in Visual Studio 2010 has got excellent in bbuilt feature to test the performance of your application

 From Debug------> Select start Performance Analysis---> Select your solution,

 Now when you run the solution in the behind you can start this and pause or stop it as and when required

It will give you a detailed report of all the Functions and how much time each function has taken to execute

Now check the functions which have taken more time and try to tune your functions and see if there are any unnecessary method calls

ANOTHER way is to use CLR Profiler to check if there are any memory leaks

you can download and use it form this link



Anoop S replied to dessiy martin on 08-Aug-11 01:40 AM

Try finding where the bottleneck lies.  

For analyzing your back-end performance, run Sql Profiler while you are running your application

To analyze code inefficiencies, use Visual Studio 2008 Code Profiler or ANTS Profiler -

For improving front-end performance, use this checklist - http://developer.yahoo.com/performance/rules.html

Verify that you have set debug=”false” for the compilation switch in the application’s web.config file and not <compilation debug=”true”/>

Just like http://mvark.blogspot.com/2008/07/how-to-make-web-pages-cross-browser.html, speeding up your ASP.NET AJAX based web application requires an understanding of it's internal aspects - http://mvark.blogspot.com/2009/03/how-to-speed-up-aspnet-ajax-web-app.html

One of the best resources for tips on improving Performance and Scalability is the MS Pattern and Practices guide - "Improving .NET Application Performance and Scalability"

You can download it from this link : http://www.microsoft.com/downloads/details.aspx?FamilyId=8A2E454D-F30E-4E72-B531-75384A0F1C47&displaylang=en or read it online here - http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag/html/scalenet.asp

The guide is very exhaustive and I recommend that you cross check your website against the checklists - http://msdn.microsoft.com/en-us/library/ms998586.aspx

Also check these links -

Well for a start don't use so many third party untested plugins. If you can't gurantee for it's efficiency you should create that bit of functionality yourself as light weight as possible. Also AJAX toolkit tends to increase the Page size and you should also consider the Viewstate as well which also adds a lot of execution time on the page. Now since we have no clue what your code or pages look like these are just some general pointers on what to do. Apply what best suits your case.
robert alis replied to dessiy martin on 08-Aug-11 01:46 AM
We need frequent data updations in real time updating application like online treading and like these others. And to achieve this DataBase Performance and Application Performance is must. To make application performance more better I will suggest you to go for dapfor .net grid which is capable in http://www.dapfor.com/Feature.aspx?id=updates like real-time data updating, real-time data highlighting, real-time data sorting and real -time data filtering.

dipa ahuja replied to dessiy martin on 08-Aug-11 03:57 AM
Untitled document
The reason behind the slow perfomance is because of our code of database operations:

To increase permanence you have to do following:
Index the table so the data can retrieve very fastly.
Use the Data access layer so that you can separate the code file and separate the design and code and make it simple to understand
Open the connection to the database only when you will require it and use the connection object locally don't declare or initialize it public..
Make sure you have close the connection after using it so the connection to the database should be removed from the memory after use
Radhika roy replied to dessiy martin on 08-Aug-11 11:38 AM


Here are the some use full suggestion to make your application super fast.

  1. Always set debug=”false” in web.config production environment.
  2. Always set trace=”false” in web.config production environment
  3. If you are using http://asp.net/ 2.0 or higher version then always use precompiled version of your code and also prefer web application project over website. If you are using website then always publish it and then upload that precompiled version of your site in production environment.
  4. Always compile your project in Release Mode before uploading application to production environment.
  5. Decrease your html kb as much as you can for that use tables less html using div’s and if possible then do not give big name to your control it will increase your html kb as http://asp.net/ uses client Id to differentiate all the controls. If you are creating custom controls then you can overwrite your clientid and uniqueId.
  6. Use cache api as much as possible it will decrease your server roundtrip and boost application performance. http://asp.net/ 2.0 or higher version provides functionality called sqlcachedependancy for your database caching. It will validate cache with your database operation like insert,update and delete and if not possible with it then use the file base caching.
  7. Remove blank spaces from your html it will increase your kb. You can use regular expression to remove white spaces. I will post the code for removing white spaces next posts.
  8. For http://asp.net/ 2.0 and higher version use master pages. It will increase your performance.
  9. Prefer database reader over dataset unless and until you have specific reason to use database.
  10. Use http://ado.net/ asynchronous calls for http://ado.net/ methods. http://asp.net/ 2.0 or higher version is supporting your performance. If you are using same procedure or command multiple time then use http://ado.net/ Prepare command it will increase your performance.
  11. Do IIS performance tuning as per your requirement.
  12. Disable view state for your controls if possible. If you are using http://asp.net/ 2.0 or higher version then use http://asp.net/ control state instead of view state. Store view state in session or database by overriding the default methods for storing view state.
  13. User Server.Transfer instead of response.redirect.
  14. Always use inproc session state if possible.
  15. Use Ajax for your application wisely. Lots of Ajax calls for a page will also decrease your performance.
  16. Measure your application performance with tools like redgate profiler,firebug and whyslovw from yahoo.
  17. User System.Text.StringBuilder for string concatenation its 4 times more faster then the normal strings.
  18. Right JavaScript in .Js files and place it as bottom of the application.
  19. Use Separate CSS files for styling of application.
  20. User database paging over normal paging while displaying huge amount of data.
  21. Call web service from java script instead of server side. Use asynchronous calls to call a web method from web service.

Hope this will help you.

Radhika roy replied to dessiy martin on 08-Aug-11 11:39 AM

 These tips can be followd during deploying ur app on the production server

A Foreword is that, these are general tips and are not exhaustive and its always better to share any new thoughts whenever we arrive at them, which will benefit the rest of the community.

Now, lets drill down into the Tips :)

In the Application Web.Config File

1. Set debug=false under compilation as follows:-

<compilation defaultLanguage="c#" debug="false">

When you create the application, by default this attribute is set to "true" which is very useful while developing. However, when you are deploying your application, always set it to "false"

Setting it to "true" requires the pdb information to be inserted into the file and this results in a comparatively larger file and hence processing will be slow.

Therefore, always set debug="false" before deployment.

2. Turn off Tracing unless until required.

Tracing is one of the wonderful features which enable us to track the application's trace and the sequences. However, again it is useful only for developers and you can set this to "false" unless you require to monitor the trace logging. You can turn off tracing as follows:-

<trace enabled="false" requestLimit="10" pageOutput="false" traceMode="SortByTime" localOnly="true"/>

3. Turn off Session State, if not required.

http://asp.net/ Manages session state automatically. However, in case you dont require Sessions, disabling it will help in improving the performance. 

You may not require seesion state when ur pages r static or whn u dont need to store infor captured in the page.

You can turn off session state as follows:-

<sessionstate timeout="20" cookieless="false" mode="Off" stateconnectionstring="tcpip=" sqlconnectionstring="data source=;Trusted_Connection=no">

While developing using Visual Studio.NET

1. Select the Release mode before making the final Build for your application. This option is available in the Top Frame just under theWindow Menu option. By default, the Mode is Debug

There are several things happening when you Build/Re-Build applications in the Debug Mode. First of all, it creates an additional PDB File under your BIN directory. This holds all the Debug information.

Secondly, the Timeout is very high since you require higher time out frequency while debugging such that your process hangs on until you get to the exact break point where error is there.

So, selecting Release Mode will greatly improve the performance of the application when u deploy.

General Methods to improve the Performance

1. Disable ViewState as and when not required.
ViewState is a wonderful technique which preserves the state of your form and controls. However, its a overhead since all the information needs to be stored in the viewstate and particularly if you are building applications which target Dial Up Internet Connection Users, ViewState can make your application very slow. In case you dont require viewstate, disable it.

You can disable it at different levels ex., for Page, Control etc., by setting 

2. Avoid Frequent round trips to the Database.
Calls made to Database can be quite expensive in terms of response time as well as resources and it can be avoided by using Batch Processing.

Make calls to Database as mininal as possible and make them last even lesser time. Use of DataAdapter wherever applicable is very useful since, it automatically opens and closes Connection whenever required and doesnt require user to explicitly open the connection.

A number of connections opened and not closed adequately can directly influence in performance slow down.

3. Avoid Throwing Exceptions.
Exceptions are a greate way to handle errors that occur in your application logic. However, throwing exceptions is a costly resource and must be avoided. Use specific exceptions and use as minimal as possible to avoid resource overhead.

For example, catching a SQLException is better when you expect only those kind of exceptions instead of a generic Exception.

4. Use Caching to improve the performance of your application.
OutputCaching enables your page to be cached for specific duration and can be made invalid based on various paramters that can be specified. The Cache exists for the duration you specify and until that time, the requests do not go to the server and are served from the Cache.

Do not assign cached items a short expiration. Items that expire quickly cause unnecessary turnover in the cache and frequently cause more work for cleanup code and the garbage collector.

In case you have static as well as dynamic sections of your page, try to use Partial Caching (Fragment Caching) by breaking up your page into user controls and specify Caching for only those Controls which are more-or-less static.

For more details regarding caching look into 

5. Use appropriate Authentication Mechanism.
The Authentication Mechanism you choose determines the cost associated with it and hence select the appropriate mechanism. An informal but useful order is as follows:-

Authentication Modes

1. None
2. Windows
3. Forms
4. Passport

6. Validate all Input received from the Users.
User Input is Evil and it must be thoroughly validated before processing to avoid overhead and possible injections to your applications. Use Client Side Validations as much as possible. However, do a check at the Server side too to avoid the infamous Javascript disabled scenarios.

7. Use Finally Method to kill resources.
In your Try..Catch.. Block, always use the Finally method to close Open connections, Open DataReaders, Files and other resources such that they get executed independent of whether the code worked in Try or went to Catch.

The Finally method gets executed independent of the outcome of the Block.

8. The String and Stringbuilder Magic.
Perhaps the most ignored type in .NET is the stringbuilder. I am sure many of us are not even aware of Stringbuilder and its advantage over string (atleast I didnt know for 1 year :))

String is Evil when you want to append and concatenate text to your string. In other words, if you are initially creating a string say s = "Hello". Then you are appending to it as s = s + " World"; You are actually creating two instances of string in memory. Both the original as well as the new string will be stored in the memory. For that matter, all the activities you do to the string are stored in the memory as separate references and it must be avoided as much as possible.

Use StringBuilder which is very useful in these kind of scenarios. For the example above, using a StringBuilder as s.Append(" World"); which only stores the value in the original string and no additional reference is created.

9. Avoid Recursive Functions / Nested Loops
These are general things to adopt in any programming language, which consumes lot of memory. Always avoid Nested Loops, Recursive functions, to improve performance.

Having said that, proper functions and call backs do increase the performance instead of having a huge chunk of lines of code in single method.

The above are just pointers to improve the performance of your application and are just illustrative. There are many more ways in which you can improve the performance of your applications. I havent dealt with IIS and SQL Server side tips to improve performance which I would explain in my forthcoming articles.

The above are my view and a collective response from various resources and you are welcome to share your views / corrections if any.

Hope this will help you.