1, to avoid the use of generic packing, unpacking operation.
GC packing operation will cause pressure; if occurs in the collection should be used to avoid generic collections.
For Collection of values, using List <T> instead of the ArrayList using Dictionary <TKey, TValue> instead Hashtable.
ArrayList h=new ArrayList(); //不建议
h.Add(1);
List<object> h = new List<object>(); //不建议
h.Add(1);
List<int> h = new List<int>(); //建议
h.Add(1);
2, using an empty string string.Empty variable assigned to the initial value
String.Empty is one referent, and "" is the concrete realization
string filter=“”;//不建议
stringfilter=string.Empty; //建议
3, StringBuilder string splicing operation
To construct a long string of 10 times (experience value) in particular over the splice should be used to make the string splicing operation StringBuilder
//不建议:
string s = null;
for (int i = 0; i < 10000; i++)
{
s += i;
}
//建议:
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 10000; i++)
{
sb.Append(i);
}
string t = sb.ToString();
4, create a StringBuilder specify the initial size of
the default initial size is 16, once more than once and increase the need Resize GC pressure. Recommendation specifies the initial size is based on experience.
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 10; i++)
{
sb.Append(i);
}
string s = sb.ToString();
//建议修改为
StringBuilder sb = new StringBuilder(256);
for (int i = 0; i < 10; i++)
{
sb.Append(i);
}
string s = sb.ToString();
5, to avoid abuse StringBuilder (note, but I have not used)
Similarly str1 + str2 + str3 + str4 string splicing operation will be compiled as String.Concat (str1, str2, str3, str4), but the efficiency is higher than StringBuilder. String.Concat string length will be determined once, StringBuilder needs to be done Resize, applicable to the case of multiple objects generated string.
By directly .Length = 0 is initialized StringBuilder.According to the results, when a plurality of times using the same StringBuilder object, provided directly by .Length = 0 to initialize the fastest.
StringBuiler sb = new StringBuilder(256);
......
sb.Remove(0, sb.Length); //不建议
sb.Length = 0; //建议
6, the value of the complex type variable as a parameter can be used to improve efficiency reference
value type from the call stack allocation allocated from a managed heap reference type. When the method is used as a value type parameter, default parameter values are copied.
//不建议
static void PrintDateTime(DateTime dt)
{
Console.WriteLine(dt);
}
//建议
static void PrintDateTime(ref DateTime dt)
{
Console.WriteLine(dt);
}
7, using ItemArray achieve DataRow batch assignment
When all fields DataRow assignment, use the field name to the column assignment by low efficiency. At this time we should try to use bulk field assignment.
You can use ItemArray or rows.Add method:
// ds是数据集(DataSet)对象
DataTable dt = ds.Tables[0];
DataRow row = dt.NewRow();
row.ItemArray = new object[] { value1, value2, …, valuen };
// ds是数据集(DataSet)对象
DataTable dt = ds.Tables[0];
dt.Rows.Add(value1, value2, …, valuen);
You should avoid doing a lot of continuous single assignment, as follows:
DataTable dt = ds.Tables[0];
DataRow row = dt.NewRow();
row["col1"] = value1;
row["col2"] = value2;
…
row["coln"] = valuen;
8, the rational use of parallel computing DataTable
DataTable built parallel computing can take advantage of the computer for each CPU, functions to optimize efficiency.
IEnumerable<DataRow> FindRows() //查找所有数量小于0的分录
{
DataTable dt = ItemDataTable;
……
return dt.Select(“Quantity<0”); //未使用并行计算
}
IEnumerable<DataRow> FindRows() //查找所有数量小于0的分录
{
DataTable dt = ItemDataTable;
……
int index = dt.Columns.IndexOf("Quantity");
return dt.AsEnumerable().AsParallel().Where(dr => (decimal)dr[index] < 0); //使用并行计算:
}
According to experiments, when the row selection of the parallel computing DataTable Select superior filtration and circulation mode; when the rows in the similar properties.
9, the merger to achieve ImportRow use the same structure DataTable
DataTable[] srcTables= ... ;
foreach(DataTable src in srcTables)
{
dest.Merge( src ) ;
}
DataTable[] srcTables = ... ;
foreach(DataTable src in srcTables )
{
foreach(DataRow row in src.Rows)
{
dest.ImportRow( row ) ;
}
}
10、
Reproduced in: https: //my.oschina.net/cjkall/blog/195846