網上也有很多人對這些做了性能對比.但不想只參照網上的,我還是要自己做一下性能對比.畢竟每個人的環境都不太一樣,而且你要把EF作為自己項目中使用,你首先要對這性能要負責.怎么能隨便摘抄一下網上的性能分析呢.
好了,我首先介紹一下我的測試環境. 數據庫 sql server 2008 R2,系統 win 7 . cpu I5. 內存4 g. 數據庫裝在本地. 我的用的是EF5.0. 數據庫的數據,已經有12萬多條了.
下面我測試的代碼
首先EF 的測試代碼

1 public class EntityDB 2 { 3 4 //根據id 查詢 5 public static void getByID() 6 { 7 using (Entities ec = new Entities()) 8 { 9 10 //id是數據庫中的任意一個id 11 var id = Parameter.IDs[Parameter.index]; 12 ec.TestEnt.Where(ent => ent.ID ==id).ToList(); 13 14 15 } 16 } 17 //求平均數 18 public static void avg() 19 { 20 using (Entities ec = new Entities()) 21 { 22 23 var id = Parameter.IDs[Parameter.index]; 24 ec.TestEnt.Average(ent => ent.Amount); 25 26 } 27 } 28 //添加 29 public static void add() 30 { 31 using (Entities ec = new Entities()) 32 { 33 TestEnt detail = new TestEnt(); 34 detail.ID = Guid.NewGuid(); 35 detail.IsPercent = true; 36 detail.Memo = "hsdgsg"; 37 detail.ModificaitonUser = "jake"; 38 detail.ModificationDate = DateTime.Now; 39 detail.Month = 11; 40 detail.MonthlyDataID = Guid.NewGuid(); 41 detail.Year = 2012; 42 detail.Status = 1; 43 ec.TestEnt.AddObject(detail); 44 ec.SaveChanges(); 45 } 46 } 47 //修改 48 public static void update() 49 { 50 using (Entities ec = new Entities()) 51 { 52 53 var id = Parameter.IDs[Parameter.index]; 54 var detail=ec.TestEnt.Where(ent => ent.ID == id).ToList().First(); 55 detail.IsPercent = true; 56 detail.Memo = "hsdgsg"; 57 detail.ModificaitonUser = "jake"; 58 detail.ModificationDate = DateTime.Now; 59 detail.Month = 11; 60 detail.Year = 2012; 61 detail.Status = 1; 62 ec.SaveChanges(); 63 } 64 } 65 //求最大值 66 public static void max() 67 { 68 using (Entities ec = new Entities()) 69 { 70 71 var id = Parameter.IDs[Parameter.index]; 72 ec.TestEnt.Max(ent => ent.Amount); 73 74 } 75 } 76 }
然后是ado.net 的測試代碼

1 public class SQLNet 2 { 3 private static string connStr = "data source=.;initial catalog=testDb;integrated security=True;MultipleActiveResultSets=True;"; 4 public static void avg() 5 { 6 7 using (SqlConnection conn = new SqlConnection(connStr)) 8 { 9 10 string sql = @"SELECT 11 AVG([Extent1].[Amount]) AS [A1] 12 FROM [dbo].[testEnt] AS [Extent1]"; 13 SqlCommand command = new SqlCommand(sql, conn); 14 SqlDataAdapter da = new SqlDataAdapter(command); 15 DataSet ds = new DataSet(); 16 if (conn.State != ConnectionState.Open) 17 { 18 conn.Open(); 19 } 20 da.Fill(ds); 21 command.Parameters.Clear(); 22 23 } 24 25 26 } 27 28 public static Guid getByID() 29 { 30 string sql=string.Format( @"SELECT [ID] 31 ,[MonthlyDataID] 32 ,[AccountCode] 33 ,[AccountName] 34 ,[Amount] 35 ,[Year] 36 ,[Month] 37 ,[Class] 38 ,[Appendix] 39 ,[DealerCode] 40 ,[DataStatus] 41 ,[Memo] 42 ,[IsPercent] 43 ,[UploadStatus] 44 ,[Status] 45 ,[CreationUser] 46 ,[CreationDate] 47 ,[ModificaitonUser] 48 ,[ModificationDate] 49 FROM [BMW.Interface].[dbo].[testEnt] 50 where [ID]=@id"); 51 Guid id = Parameter.IDs[Parameter.index]; 52 using (SqlConnection conn = new SqlConnection(connStr)) 53 { 54 SqlCommand command = new SqlCommand(sql, conn); 55 56 SqlParameter param = new SqlParameter("id", id); 57 command.Parameters.Add(param); 58 SqlDataAdapter da = new SqlDataAdapter(command); 59 DataSet ds = new DataSet(); 60 if (conn.State != ConnectionState.Open) 61 { 62 conn.Open(); 63 } 64 da.Fill(ds); 65 command.Parameters.Clear(); 66 } 67 return id; 68 } 69 70 public static void update() 71 { 72 var id=getByID(); 73 string sql=@"update [dbo].[testEnt] 74 set [Year] = @0, [Month] = @1, [Memo] = @2, [IsPercent] = @3, [Status] = @4, [ModificaitonUser] = @5, [ModificationDate] = @6 75 where ([ID] = @7)"; 76 using (SqlConnection conn = new SqlConnection(connStr)) 77 { 78 SqlCommand command = new SqlCommand(sql, conn); 79 SqlParameter param0 = new SqlParameter("0", 2012); 80 SqlParameter param1 = new SqlParameter("1", 11); 81 SqlParameter param2 = new SqlParameter("2", "hsdgsg"); 82 SqlParameter param3 = new SqlParameter("3", 1); 83 SqlParameter param4 = new SqlParameter("4", 1); 84 SqlParameter param5 = new SqlParameter("5", "jake"); 85 SqlParameter param6 = new SqlParameter("6", DateTime.Now); 86 SqlParameter param7 = new SqlParameter("7", id); 87 command.Parameters.Add(param0); 88 command.Parameters.Add(param1); 89 command.Parameters.Add(param2); 90 command.Parameters.Add(param3); 91 command.Parameters.Add(param4); 92 command.Parameters.Add(param5); 93 command.Parameters.Add(param6); 94 command.Parameters.Add(param7); 95 96 if (conn.State != ConnectionState.Open) 97 { 98 conn.Open(); 99 } 100 command.ExecuteNonQuery(); 101 command.Parameters.Clear(); 102 } 103 104 } 105 106 public static void max() 107 { 108 string sql = @" SELECT MAX([Amount]) AS [A1] FROM [dbo].[testEnt]"; 109 using (SqlConnection conn = new SqlConnection(connStr)) 110 { 111 SqlCommand command = new SqlCommand(sql, conn); 112 113 if (conn.State != ConnectionState.Open) 114 { 115 conn.Open(); 116 } 117 command.ExecuteNonQuery(); 118 command.Parameters.Clear(); 119 } 120 } 121 122 public static void add() 123 { 124 string sql = @"insert [dbo].[testEnt]([ID], [MonthlyDataID], [AccountCode], [AccountName], [Amount], [Year], [Month], [Class], [Appendix], [DealerCode], [DataStatus], [Memo], [IsPercent], [UploadStatus], [Status], [CreationUser], [CreationDate], [ModificaitonUser], [ModificationDate]) 125 values (@0, @1, null, null, null, @2, @3, null, null, null, null, @4, @5, null, @6, null, null, @7, @8)"; 126 using (SqlConnection conn = new SqlConnection(connStr)) 127 { 128 SqlCommand command = new SqlCommand(sql, conn); 129 SqlParameter param0 = new SqlParameter("2", 2012); 130 SqlParameter param1 = new SqlParameter("3", 11); 131 SqlParameter param2 = new SqlParameter("4", "hsdgsg"); 132 SqlParameter param3 = new SqlParameter("5", 1); 133 SqlParameter param4 = new SqlParameter("6", 1); 134 SqlParameter param5 = new SqlParameter("7", "jake"); 135 SqlParameter param6 = new SqlParameter("8", DateTime.Now); 136 SqlParameter param7 = new SqlParameter("0", Guid.NewGuid()); 137 SqlParameter param8 = new SqlParameter("1", Guid.NewGuid()); 138 command.Parameters.Add(param0); 139 command.Parameters.Add(param1); 140 command.Parameters.Add(param2); 141 command.Parameters.Add(param3); 142 command.Parameters.Add(param4); 143 command.Parameters.Add(param5); 144 command.Parameters.Add(param6); 145 command.Parameters.Add(param7); 146 command.Parameters.Add(param8); 147 148 if (conn.State != ConnectionState.Open) 149 { 150 conn.Open(); 151 } 152 command.ExecuteNonQuery(); 153 command.Parameters.Clear(); 154 } 155 156 } 157 }
后面我是用我自己做的一個壓力測試工具測試出來 的性能數據.
請大家參考.
方法[entityFramework_getbyID]: 成功數1000 ,失敗數0 ,完成數1000 ,平均時間2.23 毫秒,
方法[entityFramework_getbyID]: 成功數10000 ,失敗數0 ,完成數10000 ,平均時間1.89 毫秒,
方法[sqlDB_getByID ]: 成功數1000 ,失敗數0 ,完成數1000 ,平均時間0.32 毫秒,
方法[sqlDB_getByID ]: 成功數10000 ,失敗數0 ,完成數10000 ,平均時間0.35 毫秒,
方法[entityFramework_add ]: 成功數1000 ,失敗數0 ,完成數1000 ,平均時間11.93 毫秒,
方法[entityFramework_add ]: 成功數10000 ,失敗數0 ,完成數10000 ,平均時間20.90 毫秒,
方法[sqlDB_add ]: 成功數1000 ,失敗數0 ,完成數1000 ,平均時間16.28 毫秒,
方法[sqlDB_add ]: 成功數10000 ,失敗數0 ,完成數10000 ,平均時間18.62 毫秒,
方法[sqlDB_upd ]: 成功數1000 ,失敗數0 ,完成數1000 ,平均時間4.12 毫秒,
方法[sqlDB_upd ]: 成功數10000 ,失敗數0 ,完成數10000 ,平均時間135.65 毫秒,
方法[entityFramework_update]: 成功數1000 ,失敗數0 ,完成數1000 ,平均時間13.17 毫秒,
方法[entityFramework_update]: 成功數10000 ,失敗數0 ,完成數10000 ,平均時間11.46 毫秒,
方法[sqlDB_avg ]: 成功數851 ,失敗數149 ,完成數1000 ,平均時間10663.90毫秒,
方法[sqlDB_avg ]: 成功數100 ,失敗數0 ,完成數100 ,平均時間1792.23毫秒,
方法[entityFramework_avg ]: 成功數823 ,失敗數177 ,完成數1000 ,平均時間10316.37毫秒,
方法[entityFramework_avg ]: 成功數100 ,失敗數0 ,完成數100 ,平均時間1574.56毫秒,
方法[sqlDB_max ]: 成功數1000 ,失敗數0 ,完成數1000 ,平均時間7713.51毫秒,
方法[sqlDB_max ]: 成功數100 ,失敗數0 ,完成數100 ,平均時間1365.35毫秒,
方法[entityFramework_max ]: 成功數100 ,失敗數0 ,完成數100 ,平均時間680.14 毫秒,
方法[entityFramework_max ]: 成功數1000 ,失敗數0 ,完成數1000 ,平均時間8180.12毫秒,
從上面的數據,我們可以看出,其實大部分他們之間的時間都差的不是很遠.除了根據用戶id查詢這一項.
因此,我覺的用EF做大項目,是可以考慮的.不知道各位博友,有何高見.