本机测试结果如下:
大部分情况下,string 性能并不比StringBuilder差,只有特殊情况才出现差异,并非 如前面有些朋友测试的结果哪样,只要使用StringBuilder 就一定比String表现优异。
另外,大多数测试者都没有考虑内存分配的时间与成本(因为大多数的内存都比较在>2G)。
本次测试也只考虑 时间运行 效率,没有考虑空间成本
本次测试条件:.net 4.0 硬件:Tinkpad t540P,4G内存 win8.1
第一种方法:
1 [TestClass]
2 public class StringTest1
3 {
4 [TestMethod]
5 public void String1Test1()
6 {
7 for ( int i = 0; i < 1; i++)
8 {
9 string queryString = @" select p from DALParty as p where p.OwnerID=:OwnerID ";
10 queryString += " and (p.Name like :Name or p.Code like :Name or p.TaxCode like :Name) ";
11 queryString += " order by p.Name ";
12 }
13 }
14 [TestMethod]
15 public void String10Test1()
16 {
17 for ( int i = 0; i < 10; i++)
18 {
19 string queryString = @" select p from DALParty as p where p.OwnerID=:OwnerID ";
20 queryString += " and (p.Name like :Name or p.Code like :Name or p.TaxCode like :Name) ";
21 queryString += " order by p.Name ";
22 }
23 }
24 [TestMethod]
25 public void String100Test1()
26 {
27 for ( int i = 0; i < 100; i++)
28 {
29 string queryString = @" select p from DALParty as p where p.OwnerID=:OwnerID ";
30 queryString += " and (p.Name like :Name or p.Code like :Name or p.TaxCode like :Name) ";
31 queryString += " order by p.Name ";
32 }
33 }
34 [TestMethod]
35 public void String1000Test1()
36 {
37 for ( int i = 0; i < 1000; i++)
38 {
39 string queryString = @" select p from DALParty as p where p.OwnerID=:OwnerID ";
40 queryString += " and (p.Name like :Name or p.Code like :Name or p.TaxCode like :Name) ";
41 queryString += " order by p.Name ";
42 }
43 }
44 [TestMethod]
45 public void String10000Test1()
46 {
47 for ( int i = 0; i < 10000; i++)
48 {
49 string queryString = @" select p from DALParty as p where p.OwnerID=:OwnerID ";
50 queryString += " and (p.Name like :Name or p.Code like :Name or p.TaxCode like :Name) ";
51 queryString += " order by p.Name ";
52 }
53 }
54
55 [TestMethod]
56 public void StringBuilder1Test1()
57 {
58 for ( int i = 0; i < 1; i++)
59 {
60 StringBuilder ssb = new StringBuilder();
61 ssb.Append( @" select p from DALParty as p where p.OwnerID=:OwnerID ");
62 ssb.Append( " and (p.Name like :Name or p.Code like :Name or p.TaxCode like :Name) ");
63 ssb.Append( " order by p.Name ");
64 }
65 }
66 [TestMethod]
67 public void StringBuilder10Test1()
68 {
69 for ( int i = 0; i < 10; i++)
70 {
71 StringBuilder ssb = new StringBuilder();
72 ssb.Append( @" select p from DALParty as p where p.OwnerID=:OwnerID ");
73 ssb.Append( " and (p.Name like :Name or p.Code like :Name or p.TaxCode like :Name) ");
74 ssb.Append( " order by p.Name ");
75 }
76 }
77 [TestMethod]
78 public void StringBuilder100Test1()
79 {
80 for ( int i = 0; i < 100; i++)
81 {
82 StringBuilder ssb = new StringBuilder();
83 ssb.Append( @" select p from DALParty as p where p.OwnerID=:OwnerID ");
84 ssb.Append( " and (p.Name like :Name or p.Code like :Name or p.TaxCode like :Name) ");
85 ssb.Append( " order by p.Name ");
86 }
87 }
88 [TestMethod]
89 public void StringBuilder1000Test1()
90 {
91 for ( int i = 0; i < 1000; i++)
92 {
93 StringBuilder ssb = new StringBuilder();
94 ssb.Append( @" select p from DALParty as p where p.OwnerID=:OwnerID ");
95 ssb.Append( " and (p.Name like :Name or p.Code like :Name or p.TaxCode like :Name) ");
96 ssb.Append( " order by p.Name ");
97 }
98 }
99 [TestMethod]
100 public void StringBuilder10000Test1()
101 {
102 for ( int i = 0; i < 10000; i++)
103 {
104 StringBuilder ssb = new StringBuilder();
105 ssb.Append( @" select p from DALParty as p where p.OwnerID=:OwnerID ");
106 ssb.Append( " and (p.Name like :Name or p.Code like :Name or p.TaxCode like :Name) ");
107 ssb.Append( " order by p.Name ");
108 }
109 }
110 }
2 public class StringTest1
3 {
4 [TestMethod]
5 public void String1Test1()
6 {
7 for ( int i = 0; i < 1; i++)
8 {
9 string queryString = @" select p from DALParty as p where p.OwnerID=:OwnerID ";
10 queryString += " and (p.Name like :Name or p.Code like :Name or p.TaxCode like :Name) ";
11 queryString += " order by p.Name ";
12 }
13 }
14 [TestMethod]
15 public void String10Test1()
16 {
17 for ( int i = 0; i < 10; i++)
18 {
19 string queryString = @" select p from DALParty as p where p.OwnerID=:OwnerID ";
20 queryString += " and (p.Name like :Name or p.Code like :Name or p.TaxCode like :Name) ";
21 queryString += " order by p.Name ";
22 }
23 }
24 [TestMethod]
25 public void String100Test1()
26 {
27 for ( int i = 0; i < 100; i++)
28 {
29 string queryString = @" select p from DALParty as p where p.OwnerID=:OwnerID ";
30 queryString += " and (p.Name like :Name or p.Code like :Name or p.TaxCode like :Name) ";
31 queryString += " order by p.Name ";
32 }
33 }
34 [TestMethod]
35 public void String1000Test1()
36 {
37 for ( int i = 0; i < 1000; i++)
38 {
39 string queryString = @" select p from DALParty as p where p.OwnerID=:OwnerID ";
40 queryString += " and (p.Name like :Name or p.Code like :Name or p.TaxCode like :Name) ";
41 queryString += " order by p.Name ";
42 }
43 }
44 [TestMethod]
45 public void String10000Test1()
46 {
47 for ( int i = 0; i < 10000; i++)
48 {
49 string queryString = @" select p from DALParty as p where p.OwnerID=:OwnerID ";
50 queryString += " and (p.Name like :Name or p.Code like :Name or p.TaxCode like :Name) ";
51 queryString += " order by p.Name ";
52 }
53 }
54
55 [TestMethod]
56 public void StringBuilder1Test1()
57 {
58 for ( int i = 0; i < 1; i++)
59 {
60 StringBuilder ssb = new StringBuilder();
61 ssb.Append( @" select p from DALParty as p where p.OwnerID=:OwnerID ");
62 ssb.Append( " and (p.Name like :Name or p.Code like :Name or p.TaxCode like :Name) ");
63 ssb.Append( " order by p.Name ");
64 }
65 }
66 [TestMethod]
67 public void StringBuilder10Test1()
68 {
69 for ( int i = 0; i < 10; i++)
70 {
71 StringBuilder ssb = new StringBuilder();
72 ssb.Append( @" select p from DALParty as p where p.OwnerID=:OwnerID ");
73 ssb.Append( " and (p.Name like :Name or p.Code like :Name or p.TaxCode like :Name) ");
74 ssb.Append( " order by p.Name ");
75 }
76 }
77 [TestMethod]
78 public void StringBuilder100Test1()
79 {
80 for ( int i = 0; i < 100; i++)
81 {
82 StringBuilder ssb = new StringBuilder();
83 ssb.Append( @" select p from DALParty as p where p.OwnerID=:OwnerID ");
84 ssb.Append( " and (p.Name like :Name or p.Code like :Name or p.TaxCode like :Name) ");
85 ssb.Append( " order by p.Name ");
86 }
87 }
88 [TestMethod]
89 public void StringBuilder1000Test1()
90 {
91 for ( int i = 0; i < 1000; i++)
92 {
93 StringBuilder ssb = new StringBuilder();
94 ssb.Append( @" select p from DALParty as p where p.OwnerID=:OwnerID ");
95 ssb.Append( " and (p.Name like :Name or p.Code like :Name or p.TaxCode like :Name) ");
96 ssb.Append( " order by p.Name ");
97 }
98 }
99 [TestMethod]
100 public void StringBuilder10000Test1()
101 {
102 for ( int i = 0; i < 10000; i++)
103 {
104 StringBuilder ssb = new StringBuilder();
105 ssb.Append( @" select p from DALParty as p where p.OwnerID=:OwnerID ");
106 ssb.Append( " and (p.Name like :Name or p.Code like :Name or p.TaxCode like :Name) ");
107 ssb.Append( " order by p.Name ");
108 }
109 }
110 }
第二种方法
1 [TestClass]
2 public class StringTest2
3 {
4 [TestMethod]
5 public void String1Test2()
6 {
7 string queryString = string.Empty;
8 for ( int i = 0; i < 1; i++)
9 {
10 queryString = string.Empty;
11 queryString = @" select p from DALParty as p where p.OwnerID=:OwnerID ";
12 queryString += " and (p.Name like :Name or p.Code like :Name or p.TaxCode like :Name) ";
13 queryString += " order by p.Name ";
14 }
15 }
16 [TestMethod]
17 public void String10Test2()
18 {
19 string queryString = string.Empty;
20 for ( int i = 0; i < 10; i++)
21 {
22 queryString = string.Empty;
23 queryString = @" select p from DALParty as p where p.OwnerID=:OwnerID ";
24 queryString += " and (p.Name like :Name or p.Code like :Name or p.TaxCode like :Name) ";
25 queryString += " order by p.Name ";
26 }
27 }
28 [TestMethod]
29 public void String100Test2()
30 {
31 string queryString = string.Empty;
32 for ( int i = 0; i < 100; i++)
33 {
34 queryString = string.Empty;
35 queryString = @" select p from DALParty as p where p.OwnerID=:OwnerID ";
36 queryString += " and (p.Name like :Name or p.Code like :Name or p.TaxCode like :Name) ";
37 queryString += " order by p.Name ";
38 }
39 }
40 [TestMethod]
41 public void String1000Test2()
42 {
43 string queryString = string.Empty;
44 for ( int i = 0; i < 1000; i++)
45 {
46 queryString = string.Empty;
47 queryString = @" select p from DALParty as p where p.OwnerID=:OwnerID ";
48 queryString += " and (p.Name like :Name or p.Code like :Name or p.TaxCode like :Name) ";
49 queryString += " order by p.Name ";
50 }
51 }
52 [TestMethod]
53 public void String10000Test2()
54 {
55 string queryString = string.Empty;
56 for ( int i = 0; i < 10000; i++)
57 {
58 queryString = string.Empty;
59 queryString = @" select p from DALParty as p where p.OwnerID=:OwnerID ";
60 queryString += " and (p.Name like :Name or p.Code like :Name or p.TaxCode like :Name) ";
61 queryString += " order by p.Name ";
62 }
63 }
64
65 [TestMethod]
66 public void StringBuilder1Test2()
67 {
68 StringBuilder ssb = new StringBuilder();
69 for ( int i = 0; i < 1; i++)
70 {
71 ssb.Clear();
72 ssb.Append( @" select p from DALParty as p where p.OwnerID=:OwnerID ");
73 ssb.Append( " and (p.Name like :Name or p.Code like :Name or p.TaxCode like :Name) ");
74 ssb.Append( " order by p.Name ");
75 }
76 }
77 [TestMethod]
78 public void StringBuilder10Test2()
79 {
80 StringBuilder ssb = new StringBuilder();
81 for ( int i = 0; i < 10; i++)
82 {
83 ssb.Clear();
84 ssb.Append( @" select p from DALParty as p where p.OwnerID=:OwnerID ");
85 ssb.Append( " and (p.Name like :Name or p.Code like :Name or p.TaxCode like :Name) ");
86 ssb.Append( " order by p.Name ");
87 }
88 }
89 [TestMethod]
90 public void StringBuilder100Test2()
91 {
92 StringBuilder ssb = new StringBuilder();
93 for ( int i = 0; i < 100; i++)
94 {
95 ssb.Clear();
96 ssb.Append( @" select p from DALParty as p where p.OwnerID=:OwnerID ");
97 ssb.Append( " and (p.Name like :Name or p.Code like :Name or p.TaxCode like :Name) ");
98 ssb.Append( " order by p.Name ");
99 }
100 }
101 [TestMethod]
102 public void StringBuilder1000Test2()
103 {
104 StringBuilder ssb = new StringBuilder();
105 for ( int i = 0; i < 1000; i++)
106 {
107 ssb.Clear();
108 ssb.Append( @" select p from DALParty as p where p.OwnerID=:OwnerID ");
109 ssb.Append( " and (p.Name like :Name or p.Code like :Name or p.TaxCode like :Name) ");
110 ssb.Append( " order by p.Name ");
111 }
112 }
113 [TestMethod]
114 public void StringBuilder10000Test2()
115 {
116 StringBuilder ssb = new StringBuilder();
117 for ( int i = 0; i < 10000; i++)
118 {
119 ssb.Clear();
120 ssb.Append( @" select p from DALParty as p where p.OwnerID=:OwnerID ");
121 ssb.Append( " and (p.Name like :Name or p.Code like :Name or p.TaxCode like :Name) ");
122 ssb.Append( " order by p.Name ");
123 }
124 }
125 }
2 public class StringTest2
3 {
4 [TestMethod]
5 public void String1Test2()
6 {
7 string queryString = string.Empty;
8 for ( int i = 0; i < 1; i++)
9 {
10 queryString = string.Empty;
11 queryString = @" select p from DALParty as p where p.OwnerID=:OwnerID ";
12 queryString += " and (p.Name like :Name or p.Code like :Name or p.TaxCode like :Name) ";
13 queryString += " order by p.Name ";
14 }
15 }
16 [TestMethod]
17 public void String10Test2()
18 {
19 string queryString = string.Empty;
20 for ( int i = 0; i < 10; i++)
21 {
22 queryString = string.Empty;
23 queryString = @" select p from DALParty as p where p.OwnerID=:OwnerID ";
24 queryString += " and (p.Name like :Name or p.Code like :Name or p.TaxCode like :Name) ";
25 queryString += " order by p.Name ";
26 }
27 }
28 [TestMethod]
29 public void String100Test2()
30 {
31 string queryString = string.Empty;
32 for ( int i = 0; i < 100; i++)
33 {
34 queryString = string.Empty;
35 queryString = @" select p from DALParty as p where p.OwnerID=:OwnerID ";
36 queryString += " and (p.Name like :Name or p.Code like :Name or p.TaxCode like :Name) ";
37 queryString += " order by p.Name ";
38 }
39 }
40 [TestMethod]
41 public void String1000Test2()
42 {
43 string queryString = string.Empty;
44 for ( int i = 0; i < 1000; i++)
45 {
46 queryString = string.Empty;
47 queryString = @" select p from DALParty as p where p.OwnerID=:OwnerID ";
48 queryString += " and (p.Name like :Name or p.Code like :Name or p.TaxCode like :Name) ";
49 queryString += " order by p.Name ";
50 }
51 }
52 [TestMethod]
53 public void String10000Test2()
54 {
55 string queryString = string.Empty;
56 for ( int i = 0; i < 10000; i++)
57 {
58 queryString = string.Empty;
59 queryString = @" select p from DALParty as p where p.OwnerID=:OwnerID ";
60 queryString += " and (p.Name like :Name or p.Code like :Name or p.TaxCode like :Name) ";
61 queryString += " order by p.Name ";
62 }
63 }
64
65 [TestMethod]
66 public void StringBuilder1Test2()
67 {
68 StringBuilder ssb = new StringBuilder();
69 for ( int i = 0; i < 1; i++)
70 {
71 ssb.Clear();
72 ssb.Append( @" select p from DALParty as p where p.OwnerID=:OwnerID ");
73 ssb.Append( " and (p.Name like :Name or p.Code like :Name or p.TaxCode like :Name) ");
74 ssb.Append( " order by p.Name ");
75 }
76 }
77 [TestMethod]
78 public void StringBuilder10Test2()
79 {
80 StringBuilder ssb = new StringBuilder();
81 for ( int i = 0; i < 10; i++)
82 {
83 ssb.Clear();
84 ssb.Append( @" select p from DALParty as p where p.OwnerID=:OwnerID ");
85 ssb.Append( " and (p.Name like :Name or p.Code like :Name or p.TaxCode like :Name) ");
86 ssb.Append( " order by p.Name ");
87 }
88 }
89 [TestMethod]
90 public void StringBuilder100Test2()
91 {
92 StringBuilder ssb = new StringBuilder();
93 for ( int i = 0; i < 100; i++)
94 {
95 ssb.Clear();
96 ssb.Append( @" select p from DALParty as p where p.OwnerID=:OwnerID ");
97 ssb.Append( " and (p.Name like :Name or p.Code like :Name or p.TaxCode like :Name) ");
98 ssb.Append( " order by p.Name ");
99 }
100 }
101 [TestMethod]
102 public void StringBuilder1000Test2()
103 {
104 StringBuilder ssb = new StringBuilder();
105 for ( int i = 0; i < 1000; i++)
106 {
107 ssb.Clear();
108 ssb.Append( @" select p from DALParty as p where p.OwnerID=:OwnerID ");
109 ssb.Append( " and (p.Name like :Name or p.Code like :Name or p.TaxCode like :Name) ");
110 ssb.Append( " order by p.Name ");
111 }
112 }
113 [TestMethod]
114 public void StringBuilder10000Test2()
115 {
116 StringBuilder ssb = new StringBuilder();
117 for ( int i = 0; i < 10000; i++)
118 {
119 ssb.Clear();
120 ssb.Append( @" select p from DALParty as p where p.OwnerID=:OwnerID ");
121 ssb.Append( " and (p.Name like :Name or p.Code like :Name or p.TaxCode like :Name) ");
122 ssb.Append( " order by p.Name ");
123 }
124 }
125 }
测试结果如下:
根据测试结果如下:
如果 字符串拼接 次数很小 <= 1000次,两者性能几乎没有差别,只有在字符本身长度大,拼接次数很大的情况下,使用StringBuilder才有意义。
通过第二次方法测试可表明,在循环体内 每次 new StringBuilder 时,是非常耗时的工作,并且new时预分配内存,也是占内存的,有时并没有使用到。
因此:使用string 还是 StringBuilder 还要看使用环境。
在大部分情况下,string 并不会对性能造成损失,使用string 即方便,也经济,仍然是字符串操作的首先。