Friday, May 25, 2012

LINQ to SQL Vs ADO.Net Performance Test - Loading a GridView


Asp.Net provides a number of Data Access Technologies, like ADO.Net, LINQ-to-SQL, Entity Framework etc, each one of these technologies has its own advantages and disadvantages, while designing an application; we need to identify the appropriate data access technology to achieve maximum efficiency.

Here we shall evaluate the data access performance of ADO.net and LINQ-to-SQL in populating a GridView with 1000 Rows and 8 Columns.
The configuration of the system used to perform the evaluation is as follows.

OS
Windows XP Professional 2002 SP3
Processor
Pentium® D 2.66 GHz
RAM
3 GB






The performance test was carried out for 25 iterations with both ADO.net and LINQ-to-SQL, the results of the test are as follows.


ADO.Net Average time:              18.28 Milliseconds
LINQ-to-SQL Average time:        24.96 Milliseconds

The test results clearly indicate that the conventional ADO.Net technology has a distinct edge over LINQ-to-SQL, when it comes to Data Loading operations. 

However LINQ-to-SQL is not to be under estimated as it has its own advantages, use the right technology at the right place.

The code used to perform the test is as follows


ADO.Net

SqlConnection objConn;
SqlCommand objCmd;
SqlDataAdapter objDA;
DataSet dsEmployee;
Stopwatch timer;
string strQuery = string.Empty;

timer = new Stopwatch();
timer.Start();

string strConn = ConfigurationManager.ConnectionStrings["EmployeesConnectionString"].ToString();
objConn = new SqlConnection(strConn);
strQuery = "SELECT * FROM Employee";
objCmd = new SqlCommand(strQuery, objConn);
objDA = new SqlDataAdapter(objCmd);
dsEmployee = new DataSet();
objDA.Fill(dsEmployee, "dtEmployee");

grdEmployees.DataSource = dsEmployee.Tables["dtEmployee"];
grdEmployees.DataBind();
timer.Stop();

lblExecutionTime.Text = "Execution Time (Milliseconds):" + timer.ElapsedMilliseconds.ToString();


LINQ to SQL

Stopwatch timer;
EmployeeClassesDataContext dbContext = new EmployeeClassesDataContext();

timer = new Stopwatch();
timer.Start();
var emp = (from e in dbContext.Employees select new { e.ID, e.Name, e.Phone, e.DOB, e.DOJ, e.Email, e.DepartmentID });

grdEmployees.DataSource = emp;
grdEmployees.DataBind();
timer.Stop();

lblExecutionTime.Text = "Execution Time (Milliseconds):" + timer.ElapsedMilliseconds.ToString();



That’s it we have evaluated the performance of ADO.net and LINQ-to-SQL in loading a GridView with 1000 Rows and 8 Columns. 




RELATED POST




Search Flipkart Products:
Flipkart.com

No comments: