Wednesday, May 30, 2012

DataSet Vs DataReader Performance Test – Loading a GridView


In this post DataSet Vs DataReader Performance Test – Loading a GridView we, shall compare the performance of DataSet and DataReader in populating a GridView.

Asp.Net provides a number of ways to fill a GridView, like DataReader, DataSet etc, each one has its own advantages and disadvantages, we know that the DataReader is best suited for forward only read operations and the DataSet is best suited for offline data updates, we shall now see the difference in the loading time between the two in action.

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 DataSet and DataReader; the results of the test are as follows.

Loading 1000 Records
DataSet Average time:             61.24 Milliseconds
DataReader Average time:       65.68 Milliseconds




Loading 2000 Records
DataSet Average time:             166.32 Milliseconds
DataReader Average time:       155.04 Milliseconds

















Loading 3000 Records
DataSet Average time:             229.04 Milliseconds
DataReader Average time:       182.34 Milliseconds
















Inference
Initially for 1000 rows, it appeared that the DataSet was performing marginally better than the DataReader, but as the number of rows increased to 2000 & later to 3000, the DataReader performed considerably better than the DataSet, thus proving the concept that the DataReader is best suited for ReadOnly forward only data.














The code used to perform the test is as follows

DataSet
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"].DefaultView;
grdEmployees.DataBind();

timer.Stop();

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

DataReader
SqlConnection objConn;
SqlCommand objCmd;
SqlDataReader objDR;
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);
objConn.Open();
objDR = objCmd.ExecuteReader();
grdEmployees.DataSource = objDR;
grdEmployees.DataBind();
objConn.Close();
timer.Stop();

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

That’s it we have seen the performance difference between the DataSet and the DataReader in loading a GridView with data.

Search Flipkart Products:
Flipkart.com

1 comment:

Alok said...

nice article Prakash.