最近一直在研究怎么超越手工代码绑定实体性能,但是还是没有实现,贴出研究过程中的产物:
public class PerformanceTest { int count = 100000; public string GetTestInfo() { StringBuilder content = new StringBuilder(); System.Diagnostics.Stopwatch oTime = new System.Diagnostics.Stopwatch(); oTime.Start(); GetDataFor(); oTime.Stop(); content.Append(" 实体赋值用时:").Append(oTime.Elapsed.Seconds).Append("秒 ").AppendLine(oTime.Elapsed.Ticks.ToString()); oTime = new System.Diagnostics.Stopwatch(); oTime.Start(); GetfsDataFor(); oTime.Stop(); content.Append(" 反射实体赋值用时:").Append(oTime.Elapsed.Seconds).Append("秒 ").AppendLine(oTime.Elapsed.Ticks.ToString()); oTime = new System.Diagnostics.Stopwatch(); oTime.Start(); GetMyTableDataFor(); oTime.Stop(); content.Append(" 我的二维表格对象实体赋值用时:").Append(oTime.Elapsed.Seconds).Append("秒 ").AppendLine(oTime.Elapsed.Ticks.ToString()); //OrderInfo uu = GetEntiy(); return content.ToString(); } #region 我的二维表格实体 private Globe.Database.TableDataEntity GetMyTableDataFor() { Globe.Database.TableDataEntity tableData = new Globe.Database.TableDataEntity(); for (int i = 0; i < count; i++) { tableData.Add(LoadMyRow()); } return tableData; } private Globe.Database.RowDataEntity LoadMyRow() { Globe.Database.RowDataEntity row = new Globe.Database.RowDataEntity(); row.Fileds.Add("Address", "这是一个地址,武汉市新洲区双柳街"); row.Fileds.Add("CategoryID", "999999"); row.Fileds.Add("Comment", "信息信息信息信息信息信息信息信息信息信息信息信息信息信息信息信息"); row.Fileds.Add("ContactName", "名字名字名字"); row.Fileds.Add("CustomerID", "1"); row.Fileds.Add("CustomerName", "名字名字名字"); row.Fileds.Add("Finished", "FinishedFinished"); row.Fileds.Add("OrderDate","2012-03-03 22:22:22 222"); row.Fileds.Add("OrderID","55555555"); row.Fileds.Add("PostalCode","2.2.2.2.2.2"); row.Fileds.Add("ProductID","343434"); row.Fileds.Add("ProductName","ProductNameProductNameProductNameProductName"); row.Fileds.Add("Quantity","QuantityQuantityQuantityQuantityQuantity"); row.Fileds.Add("Remark","RemarkRemarkRemarkRemarkRemarkRemarkRemarkRemarkRemarkRemarkRemarkRemarkRemarkRemarkRemarkRemarkRemarkRemarkRemarkRemarkRemarkRemarkRemarkRemarkRemarkRemarkRemarkRemarkRemarkRemarkRemarkRemarkRemarkRemarkRemarkRemark"); row.Fileds.Add("SumMoney","SumMoneySumMoneySumMoneySumMoneySumMoneySumMoneySumMoneySumMoneySumMoneySumMoneySumMoneySumMoney"); row.Fileds.Add("Tel","13343583268"); row.Fileds.Add("Unit","ttttttttttttttt"); row.Fileds.Add("UnitPrice","http://www.cnblogs.com/shunhe316/archive/2012/08/30/2664667.html"); return row; } #endregion #region 手工实体 private List GetDataFor() { List info = new List (); for (int i = 0; i < count; i++) { info.Add(LoadOrderInfo()); } return info; } private OrderInfo LoadOrderInfo() { OrderInfo info = new OrderInfo(); info.Address = "这是一个地址,武汉市新洲区双柳街"; info.CategoryID = "999999"; info.Comment = "信息信息信息信息信息信息信息信息信息信息信息信息信息信息信息信息"; info.ContactName = "名字名字名字"; info.CustomerID = "1"; info.CustomerName = "名字名字名字"; info.Finished = "FinishedFinished"; info.OrderDate = "2012-03-03 22:22:22 222"; info.OrderID = "55555555"; info.PostalCode = "2.2.2.2.2.2"; info.ProductID = "343434"; info.ProductName = "ProductNameProductNameProductNameProductName"; info.Quantity = "QuantityQuantityQuantityQuantityQuantity"; info.Remark = "RemarkRemarkRemarkRemarkRemarkRemarkRemarkRemarkRemarkRemarkRemarkRemarkRemarkRemarkRemarkRemarkRemarkRemarkRemarkRemarkRemarkRemarkRemarkRemarkRemarkRemarkRemarkRemarkRemarkRemarkRemarkRemarkRemarkRemarkRemarkRemark"; info.SumMoney = "SumMoneySumMoneySumMoneySumMoneySumMoneySumMoneySumMoneySumMoneySumMoneySumMoneySumMoneySumMoney"; info.Tel = "13343583268"; info.Unit = "ttttttttttttttt"; info.UnitPrice = "http://www.cnblogs.com/shunhe316/archive/2012/08/30/2664667.html"; return info; } #endregion #region 反射实体 private List GetfsDataFor() { List info = new List (); for (int i = 0; i < count; i++) { info.Add(GetEntiy < OrderInfo>()); } return info; } public T GetEntiy () where T : new() { T info = new T(); System.Reflection.PropertyInfo propertyInfo = typeof(T).GetProperty(""); //得到实体类属性的集合 PropertyInfo[] properties = typeof(T).GetProperties(); foreach (PropertyInfo fieldInfo in properties)//遍历数组 { fieldInfo.SetValue(info, "我的二维表格我的二维表格我的二维表格", null); } return info; } #endregion }
循环绑定100000次,最终运行结果:
实体手工绑定赋值用时:0秒 时间刻度:656027
反射实体绑定赋值用时:3秒 时间刻度:36066423 我的二位表格对象实体赋值用时:1秒 时间刻度:13217015