Wednesday, 9 October 2013

How to optimize code in ASP.Net and C#

Hello Friends,

Today I am writing some techniques for code optimization in ASP.Net and C#. Using this technique you will find your page has been got up speed.


Disable ViewState: You can Set "EnableViewState=false" for any control that havn't need the view state. 

Use Page.Ispostback is used in Page_Load: Write code in page_load event with this condition 
                 if(IsPostback==true)
                    {
                                    Your Code Here
                      }
This technique will reduce compiler time for compiling code again and again.

Asynchronous calls for Web Services: If you are using Web Services in your Project or Page and its take long time in loading, then you should use Asynchronous calls to Web Services where ever applicable and make sure to wait for end of the calls before the page is fully loaded. 

Use String Builder for large string operations: For any long string operations, use String Builder instead. String Builder if better for long string and for these type string which is concatenate by using loop or frequently.

Specialized Exception Handling:  Exception Handling must implemented in your application, You should use Try, Catch and Finaly blog.

 try
{
}
catch
{
}
finaly
{
}

Leave Page Buffering on:  Leave Page buffering On, unless specifically required so. where you might require to turn it on in case of very large pages so that user can view something while the complete page is loading.

Use Caching:  Cache Data whenever possible especially data which you are sure won't change at all or will be reused multiple times in the application. Make sure to have consistent cache keys to avoid any bugs. For simple pages which do not change regularly you can also go for page Caching.
Caching has many types
Page Output Caching
Application Caching

Use Script files: Do not write javascript and CSS code directly on page, We should create seperate file for this.

Remove Unused Javascript: Unusable Javascript or any others type script code should not place on page.

Remove Hidden HTML when using Tabstrip: If you are using a Tabstrip control and if the HTML size is too large and the page does frequent reloads, then turn Autopostback of Tabstrip on, put each Pageview inside a panel and turn visibility of all Panels except the current one to False. This will force the page to reload every time a tab is changed however the reload time will reduce heavily. Use your own jurisdiction to best use.

Use Div not table : We should ignore table and use div as pssible.

Disable session when not using it - Session should be place on only those page where its required otherwise remove from there.
//
// <%@ Page EnableSessionState="false" %>
//
If the page only reads session but does not write anything in session, then make it read only.
//
// '<%@ Page EnableSessionState="ReadOnly" %>

//

Use Option Strict On (VB .Net only)  Enabling Option Script restricts implicit type conversions, which helps avoid those annoying type conversion and also is a Performance helper by eliminating hidden type conversions. I agree it takes away some of you freedom, but believe me the advantages outweigh the freedom.

Use Threading: When downloading large amounts (above to MB) of data use Threading concept to load Data in background. Be know that threading does carry overhead and must be used carefully. A thread with a short lifetime is inherently inefficient, and context switching takes a significant amount of execution time. You should use the minimum number of long-term threads, and switch between them as rarely as you can.

Use Chunky Functions:  A chunky call is a function call that performs several tasks. you should try to design your application so that it doesn't rely on small, frequent calls that carry so much overhead.

Use Jagged Arrays: In case you are doing heavy use of Multi Dimensional Arrays, use Jagged Array ("Arrays of Arrays") Instead
Use "&" instead of "+" - You should use the concatenation operator (&) instead of the plus operator (+) to concatenate strings. They are equivalent only if both operands are of type String. When this is not the case, the + operator becomes late bound and must perform type checking and conversions.

Use Ajax: In performance critical application where there are frequent page loads, resort to Ajax.

Use the SqlDataReader class: The SqlDataReader class provides a means to read forward-only data stream retrieved from a SQL Server™ database. If you only need to read data then SqlDataReader class offers higher performance than the DataSet class because SqlDataReader uses the Tabular Data Stream protocol to read data directly from a database connection

Choose appropriate Session State provider:  In-process session state is the fastest solution. If you store only small data in session state, go for in-process provider. The out-of-process solutions is useful if you scale your application across multiple processors or multiple computers.

Use Stored Procedures: Stored procedures are pre-compiled and hence are much faster than a direct SQL statement call. We should avoid inline query. This technique is also useful in prevent sql injection attack.

Use Web Services with care: Web Services depending on data volume can have monstrous memory requirements. Do not go for Web Services unless your Business Models demands it.

Paging in Database Side:  When you needs to display large amount of Data to the user, go for a stored procedure based Data Paging technique instead of relying on the Data Grid/ Data List Paging functionality. Basically download only data for the current page. 

0 comments:

Post a Comment