LINQ To SQL 的效能 (列出部份的測試程式碼)

摘要:LINQ To SQL 的效能 (列出部份的測試程式碼)

一時興起,測了一下LINQ To SQL的效能,

 以100000筆資料的讀入來測試,為求平衡,特別以5次讀取之總值測試.

 

讀取方式 結果
With Un-Typed DataTable Load (use DataAdapter without Delete/Insert/Update assocation) 5188 ms
With LINQ To SQL                                                                     4203 ms

我很訝異LINQ To SQL的效能,居然呈現出比Un-Typed DataTable更快,雖然就理論上而言,

LINQ To SQL所產生的類別於成本上應比DataRow來得低,但這樣的差距能引起我的懷疑,

或許我尚有遺漏之處也說不定,待較有空時,審視一下LINQ To SQL的Fill機制,應能找到其

效能提升的證據.

 

列出部份測試的程式碼.

With UnTyped DataTable

static void TestUnTypedLoad()
 {
            SqlConnection conn = new SqlConnection(DSPerformanceTest.Properties.Settings.Default.Northwind2ConnectionString);
            DataTable table = new DataTable();
            using (conn)
            {
                using(SqlCommand cmd = new SqlCommand("SELECT * FROM TEST_TABLE",conn))
                {
                    using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
                    {                       
                        adapter.Fill(table);
                    }
                }
            }           
        }

 

With LINQ to SQL

static void TestLoadWithLinq()
 {           
            NorthwindDataContext context = new NorthwindDataContext();
            var s = (from s1 in context.TEST_TABLEs select s1).ToList();
}