Website optimization experience and skills--streamlined and efficient C #

Source: Internet
Author: User
Tags static class

For large web sites, technical coverage is very wide, on the hardware, software, programming language, Web Service, firewall and so on have very high requirements.

Faced with a large number of users, high concurrent requests, you can use High-performance servers, high-performance programming languages, high-performance databases, increased bandwidth, etc., which means huge input.

If you don't have the plan, and want to get better system performance, then we need to budget, from the "soft" aspects of the start.

If you have any of the following usage, or have different opinions, please enlighten me.

  (1) foreach has better execution efficiency than for.

foreach takes about 30% of the time, and through the test results, we recommend more efficient foreach when both are available. In addition, the time to write data in for is about 10 times times the time it takes to read data.

  (2) Avoid using ArrayList

Since any object is stored inside, it is converted to the System.Object type, and the data is removed from the ArrayList to be disassembled back to the original type. Recommended use. NET2.0 generics, which is a strong type, avoids the performance cost of a boxed unboxing.

  (3) Do not use uppercase,lowercase to convert strings, and then compare them. Instead of String.Compare, he can ignore case comparisons .

  (4) Replace the string connector "+" with StringBuilder

Reason, see my another article string and bulidstring performance comparisons and intrinsic mechanisms

  (5) Avoid declaring variables within the loop, declaring variables outside the loop, initializing in the loop

 

1//Avoid
2
3 for (int i = 0; i < i + +)
4
5 {
6
7 Anyclass cl = new Anyclass ();
8
9//...
10
11}
12
13//Recommended
for (int i = 0; i < i + +)
15
16 {
17
CL = new Anyclass ();
19
20//...
21st
22}

 (6) Catch exception, do not use System.Exception

1//Avoid
2 Try
3 {
4//TODO
5}
6 catch (Exception ex)
7 {
8//TODO (although simple, but poor performance)
9}
10
11//Recommended
Try
13 {
/TODO
15}
catch (System.NullReferenceException ex)
17 {
18//NULL Object exception handling
19}
catch (System.ArgumentOutOfRangeException ex)
21 {
22//Exceeded scope exception handling
23}
catch (System.InvalidCastException ex)
25 {
26//The handling of abnormal transformation
27}

  (7) Do not use exception control procedures.

Exception capture is a great drain on performance and is known to avoid the use of the best

1//Avoid
2
3 Try
4 {
5 result = 200/num;
6}
7 catch (Exception ex)
8 {
9 result = 0
10}
11
12//Recommended
A try
14 {
result = num!= 0? 200/num:0;
16}
catch (Exception ex)
18 {
result = 0
20}

 (8) releasing resources using using and try/finally when encountering calls to implement IDisposable objects

1//Avoid
2
3 public void Exceutecommand ()
4 {
5 SqlConnection sql = new SqlConnection (Strcon);
6 SqlCommand cmd = new SqlCommand (SQL);
7 SQL. Dispose (); There is an error, and there may never be a call here.
8 cmd. Dispose ();
9}
10
11//Suggestion
12
No object can be placed in the/using, only objects that implement the IDisposable interface can be used.
14//When the compiler is IL, the using will automatically put the content in the try/finally.
15
Try
17 {
using (SqlConnection sql = new SqlConnection (Strcon))
19 {
using (SqlCommand cmd = new SqlCommand (SQL))
21 {
/TODO
23}
24}
25}
-catch (Exception ex)
27 {
28//
29}
finally {
31//or
//SQL. Dispose ();
Per/cmd. Dispose ();
34}
35
36//Recommended
37
38 try/finally Better if you encounter multiple objects that implement the IDisposable interface need to be released
39
40//Recommended
SqlConnection sql = null;
SqlCommand cmd = null;
43
The Try
45 {
sql = new SqlConnection (Strcon);
$ cmd = new SqlCommand (SQL);
SQL. Open ();
The cmd. ExecuteNonQuery ();
50
51}
finally {
if (SQL!= null)
SQL. Dispose ();
if (cmd!= null)
The cmd. Dispose ();
57}

(9) Avoid the use of reflection, reflection is a more wasteful performance of the Operation

Calling a type or method by reflection, a field or property is something that the CLR does more work on, such as validating parameters, checking permissions, so the speed is very slow. For applications that intend to write a dynamic constructed type (late bound), they can be implemented by inheritance, interfaces, and delegates.

 (10) value types are grouped into strings, please use the. Tostrng () method to avoid boxing operations.

1//Avoid
2
3 var str = "Hello" + 1 + 2;
4
5//Recommended
6
7 var str = "Hello" + 1. ToString () + 2. ToString ();

 (one) Stopwatch class test run time

View Code
1 public delegate void AddHandler ();
2 Class Program
3 {
4 static void Main (string [] args)
5 {
6 Utility.processtimespan (Program.add);
7 Console.read ();
8}
9
Ten public static void Add ()
11 {
var a = 0;
for (int i = 0; i < 100000000 i + +)
14 {
A + +;
16}
17}
18}
19
public static Class Utility
21 {
public static void Processtimespan (AddHandler adddelegate)
23 {
24//Recommended
DateTime start = DateTime.Now;
var timer = stopwatch.startnew ();
AddDelegate ();
Timer. Stop ();
Console.WriteLine ("Method took {0} ms", timer.) Elapsedmilliseconds);
30
31//Traditional
32
DateTime start = DateTime.Now;
Somecodetotime ();
DateTime end = DateTime.Now;
Console.WriteLine ("Method took {0} ms", (End-start). TotalMilliseconds);
37
38
39
40}
41}

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.