Asp.net| optimization
- Don't rely on exceptions in your code: Exceptions are very expensive and should rarely in occur code. You are should never use exceptions as a way to the normal program flow. If it is possible into detect in the code a condition that would cause a exception and you should does that instead the waiting to CA TCH the exception before handling that condition. Common scenarios include checking for null, assigning to a string that'll be parsed into a numeric value, or checking fo R specific values before applying math operations. For example:
Consider changing this:try {result = 100/num;} catch (Exception e) {result = 0;} To This:if (num!= 0) result = 100/num;else result = 0;
' consider changing this:try result = 100/numcatch (e as Exception) result = 0End try//to this:if not (num = 0) re Sult = 100/numelse result = 0End If
Consider changing this:try {result = 100/num;} catch (e:exception) {result = 0;} To This:if (num!= 0) result = 100/num;else result = 0;
C # VB JScript
- Use early binding in Visual Basic or JScript code: One of the advantages of Visual Basic, VBScript, and JScript I s their typeless nature. Variables can is created simply by using them and need no explicit type declaration. When assigning from one type to another, conversions are performed automatically, as. This can are both an advantage and a disadvantage, since late binding are a very expensive convenience in terms of Performan Ce.
- The Visual Basic language now supports Type-safe programming through the use of a special Option Strict directive. For backward compatibility, ASP.net does not enable Option Strict by default. However, for optimal perfomance, your should enable Option Strict for your pages by using a Strict attrib Ute on the page or control directive:
<%@ Page language= "VB" strict= "true"%><%dim Bdim C as String ' This causes a compiler error:a = ' Hello ' ' This Cau Ses a compiler error:b = "World" ' this does NOT:C = '!!!!!! ' But this does:c = 0%>
- JScript also supports typeless programming, though it offers no compiler directive to force early. A variable is late-bound if:
- It is declared explicitly as a object.
- It is a field of the A class with no type declaration.
- It is a private Function/method member with no explicit type declaration and the type cannot to be inferred to.
- The last distinction is complicated. The JScript compiler optimizes if it can figure out of the type, based on how a variable is used. In the following example, the variable A are early-bound but the variable B is late-bound:
var a;var B; A = "Hello"; B = "World"; B = 0;
- For the best performance, declare your JScript variables as has a type. For example, "Var a:string".
- Port call-intensive COM components to managed code: the. NET Framework provides a remarkably easy way to interoperate with traditional COM components. The benefit is this you can take advantage of the the new platform while preserving the your code. However, there are some circumstances in which the performance cost of keeping your old components is greater than the Cos T to migrate your components to managed code. The Every situation is unique, and the best way to decide what needs to being changed to measure site. In general, however, the performance impact of COM interoperability are proportional to the number of function calls made O R The amount of data marshaled from unmanaged to managed code. A component that requires a high volume of calls to interact with it are called "chatty," due to the number of Communicatio NS between layers. You are should consider porting such components to fully managed code to benefit from the performance gains provided by the. N ET platform. Alternatively, you might consider redesigning your ComponenT to require fewer calls or to marshal more data at once.
- Use SQL stored procedures for data access: "All" the data access methods provided by the. NET Framework, Sql-bas Ed data access is the best choice for building scalable Web applications with the best performance. When using the managed SQL provider, you can get a additional performance boost by using compiled stored procedures Inste ad hoc queries. For a example of using SQL stored procedures, refer to the Server-side Data Access section of this tutorial.
- Use SqlDataReader for a fast-forward, read-only data cursor : A SqlDataReader object provides a forward, Read-only cursor over the data retrieved from SQL database. Because SqlDataReader uses tabular data Stream (TDS) packets to read data directly. A database connection, it p Rovides better performance than using a DataSet , if you can use this for your scenario. Because SqlDataReader supports the IEnumerable interface, you can even bind server controls, as. For a example of using SqlDataReader , the Server-side Data Access section of this tutorial.
- Cache data and output wherever possible : the ASP.net programming model provides a simple mechanism for caching Page output or data when it does is not need to is dynamically computed for every request. Can design your pages with caching in mind to optimize those places in your application this you expect to have the MO St Traffic. More than any feature of the. NET Framework, the appropriate use of caching can enhance the performance of your site, some Magnitude or more. For more information about the "Use caching," The Cache Services section of this tutorial.
- enable Web Gardening for multiprocessor computers: The asp.net process model helps Enable scalability on Multipro Cessor machines by distributing the work to several processes, one for each CPU, each with processor affinity set to its C Pu. The technique is called Web gardening, and can dramatically improve the performance of some applications. To learn you to enable Web gardening, refer to the Using the "Process Model section."
- Don't forget to disable Debug mode: The <compilation> section in asp.net configuration controls W Hether an application are compiled in Debug mode, or not. Debug mode degrades performance significantly. Always Remember to disable Debug mode before you deploy a production application or measure performance. For more information about Debug mode, refer to the section entitled The SDK Debugger.