Performance Optimization summary.

Source: Internet
Author: User
Tags add numbers

At the requirements of the leaders, I recently optimized the project and summarized it as follows:

 

1. Use stored procedures (if exec stored procedure parameters are used in the program, the execution does not seem to be much faster)

It is pre-compiled in the database and does not need to spend a lot of time on string transmission. Prevents SQL injection attacks.

2. Optimize database statements to make the logic as simple as possible.

@ Also, the efficiency of charindex> like> padindex decreases sequentially when the function is used.

@ When querying whether a field contains a string separated by "in", it is recommended that you do not use in for a very slow speed. There are many other things that can be summarized. I will not describe them here.

3. EnableViewState (view State of the page ). Set this parameter to false unless otherwise specified.

When ViewState is used, each object must be first serialized to ViewState, and then deserialized by returning the object. Therefore, ViewState has no cost. Minimize the number of objects used,

If possible, minimize the number of objects in ViewState. Viewstate can be disabled in the following cases:

(1) Page Control (. ascx)

(2) The page is not returned to itself.

(3) You do not need to process control events.

(4) The control does not have dynamic or data-bound attribute values (or each postpack is processed in the Code)

ViewState is disabled on a single page or every page, as shown below:

SINGLE Page: <% @ Page EnableViewState = "False" %>

Each page: In web. config, <Pages EnableViewState = "false"/>

EnableSessionState: retain the default value (if sessionstate is used on the page, it will occupy resources ).

EnableViewStateMac: If there are no special security requirements, keep the default value.

4. Pagelayout. page layout model. We recommend that you use Flowlayout (the element is not added with the absolute positioning attribute ). gridlayout (absolute positioning attribute) uses absolute positioning to produce more code than Flowlayout, mainly the positioning information of controls. Radiobuttonlist and checkboxlist

5. Remember to release the Debug status of the page when the project is released.

6. Select the html control whenever possible. Functions that can be implemented on the client are implemented on the client (proficient in javascript) to reduce the pressure on the server.

Data Control selection sequence: Repeater, DataList, and DataGrid

7. After a database connection is established, the connection is enabled only when operations are required. Close the connection immediately after use, so as to minimize the time for opening the database connection and avoid exceeding the connection limit.

8. Optimized string operation performance

Use the ToString method of the Value Type
When connecting strings, you often use the "+" sign to directly add numbers to strings. Although this method is simple, it can also get the correct results, but because it involves different data types, numbers need to be packed. To be added to a string. However, the packing operation has a great impact on the performance, because during such processing, a new object will be allocated in the managed heap, and the original value will be copied.

To the newly created object. The Value Type ToString method can be used to avoid packing and improve application performance.

Use StringBuilder class
The String class object cannot be changed. In essence, the re-assignment of the String object re-creates a String object and assigns the new value to the object. The ToString method does not improve the performance.

Significant. When processing strings, it is best to use the StringBuilder class. Its. NET namespace is System. Text. This class does not create a new object, but uses Append, Remove, Insert

And return the operation result using the ToString method.

9. cache data or page output as long as possible

ASP. NET provides some simple mechanisms to cache the output or data of these pages when you do not need to request dynamic computing page output or data for each page. In addition, by designing the pages and data to be cached, please

(Especially in areas where the site is expected to have a large amount of communication), you can optimize the performance of these pages. Compared with any Web form function of. NET Framework, it is better to use the cache properly.

This increases the performance of your site by an order of magnitude. There are two points to note when using the ASP. NET cache mechanism. First, do not cache too many items. Each item in the cache has an overhead, especially when the memory

Usage. Do not cache items that are easy to recalculate and rarely used. Second, the period of validity allocated to cached items should not be too short. Items that expire soon will lead to unnecessary turnover in the cache, and often lead to more

Code cleanup and garbage collection. If you are concerned about this issue, monitor the Cache Total Turnover Rate performance counters associated with ASP. NET Applications performance objects. High turnover rate may be said

It indicates that there is a problem, especially when the item is removed before expiration. This is also called memory pressure.

10. Use HttpServerUtility. Transfer to redirect between pages of the same application
Use the Server. Transfer syntax to avoid unnecessary client redirection by using this method on the page. However, the user of response. redirect .response.exe cute must be differentiated as needed.

Method. .
11. Use the Garbage Collector and Automatic Memory Management of the Common Language Runtime Library as appropriate
Be careful not to allocate too much memory to each request, because the Garbage Collector must do more work more frequently. In addition, do not point unnecessary pointers to objects because they will keep objects in active state and avoid objects containing the Finalize method as much as possible, because they will lead to more work in the future. In particular, in the Finalize call, never release resources because resources are recycled by the garbage collector.

Previously, the memory may have been consumed. At last, this problem often causes a devastating impact on the performance of the Web server environment, because it is easy to exhaust a specific resource while waiting for Finalize to run.

12.Do not rely on exceptions in the code

Exceptions greatly reduce performance, so you should not use them as a way to control normal program processes. If the code may cause exceptions, perform this operation. Do not capture exceptions before processing the status. Common solutions include checking null, assigning a value to a String that is analyzed as a numeric value, or checking a specific value before applying a mathematical operation. The following example demonstrates the possible

Code that causes exceptions and whether code in a certain state exists.

13. Use HttpResponse. Write to concatenate strings
This method provides very effective buffering and connection services. However, if you are executing extensive connections, use multiple Response. Write calls.

The technology shown in the following example is faster than the connection string using a single call to the Response. Write method.

Response. Write ("atest ");
Response. Write (strString );
Response. Write ("boxbig"); 14. Keep the buffer closed for special reasons
Disabling Web form page buffering can cause a large amount of performance overhead. 15. Avoid unnecessary round-trip to the server
Use Page. IsPostBack to avoid unnecessary operations on the round-trip Process
Although you may want to use the time-saving and code-saving functions of the Web forms page framework as much as possible, ASP. NET Server controls and sending back events cannot be used in some cases. Generally, you only need to start the round-trip process to the server when retrieving or storing data. Most data operations can be performed on clients during the round-trip process. 16. ASP. NET application performance test
Before performing a performance test on ASP. NET applications, ensure that the application is correctly functioning and has no errors. The following tools can be used for specific performance tests: Web Application Strees Tool (WAS) is a free test Tool released by Microsoft. It can simulate hundreds of thousands of users simultaneously to access web applications, form a traffic load on the server, so as to achieve the purpose of testing, can generate average TTFB, average TTLB and other performance summary reports. Application Center Test (ACT) is a testing tool that is attached to the Enterprise Edition of Visual Studio. NET and is a web Application testing tool officially supported by Microsoft. It can intuitively generate chart results with more functions than WAS, but does not have the ability to be tested simultaneously by multiple clients. The "performance" counter in the "Administrative Tools" of the server operating system can monitor the server to understand the application performance. Microsoft used the IIS log viewing tool LogParserLizardSetup. msi and LogParser. msi. You can view the execution time of each page load call. 17. To Compress JavaScript files, the Case sensitivity of js calls on the page must be the same, so that different files are not cached. If JavaScript on the page is acceptable, it should be written as a unit file for calling. Use less jpeg images and use gzip to compress webpages to accelerate page display. 18. Write the js call at the bottom of the page as much as possible. You can also rewrite the viewstate to the lower part of the page or compress the viewstate. The condition is that the viewstate must be used. Proper use of some optimization techniques helps program running speed and proper configuration of software and hardware. It is also a far-reaching project for developers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.