Monday, May 28, 2012

LINQ to SQL Vs ADO.Net Performance Test - INSERT


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 to execute an INSERT query with 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:             1083 Microseconds
LINQ-to-SQL Average time:       3016 Microseconds

The test results clearly indicate that ADO.Net technology has a distinct edge over LINQ-to-SQL, when it comes to INSERT 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;
Stopwatch timer;
string strQuery = string.Empty;

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

string strConn = ConfigurationManager.ConnectionStrings["EmployeesConnectionString"].ToString();
objConn = new SqlConnection(strConn);
strQuery = "INSERT INTO Employee (Name,DOB,DOJ,Phone,Email,DepartmentID,Salery) VALUES (";
strQuery += "'Tom','11/11/1967','05/01/2009','123-123-1234','tom@abcsoftware.com','1','5000')";
objCmd = new SqlCommand(strQuery, objConn);
objConn.Open();
objCmd.ExecuteNonQuery();
objConn.Close();
timer.Stop();

lblExecutionTime.Text = "Execution Time (Micro Seconds):" + (1e6 * timer.ElapsedTicks / (double)Stopwatch.Frequency).ToString();

LINQ to SQL

EmployeeClassesDataContext dbContext = new EmployeeClassesDataContext();
Stopwatch timer;
Employee objEmp = new Employee();

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

objEmp.Name = "Robert";
objEmp.DOB = "10/07/1973";
objEmp.DOJ = "05/01/2009";
objEmp.Phone = "111-222-3333";
objEmp.Email = "robert@abcsoftware.com";
objEmp.DepartmentID = 4;
objEmp.Salery = 5500;

dbContext.Employees.InsertOnSubmit(objEmp);
dbContext.SubmitChanges();

timer.Stop();

That’s it we have evaluated the performance of ADO.net and LINQ-to-SQL in executing an INSERT Query with 8 Columns. 



RELATED POST



Search Flipkart Products:
Flipkart.com

No comments: