Measure
Execution time using Environment class
Many a times we will have situations where we need to evaluate the
performance of a function or a piece of code, Performance in the database can
be measured using tools like SQL Profiler and other tools, but when it comes to
the .Net code, we need to evaluate the time elapsed to execute the code.
In .Net the System Namespace provides an Environment class, which can be used to measure the time elapsed to execute a code. The TickCount property of the class can be used to get the Start/Stop ticks using which we can measure the time Elapsed to execute the code
int startTime, endTime;
startTime = Environment.TickCount;
//Code to be
Evaluated Goes Here.
endTime = Environment.TickCount;
endTime = Environment.TickCount;
endTime -
startTime – Gives the time taken to execute the code in Milliseconds.
Example
Let us measure
the time taken to load a DropDownList with 1000 items; here is
the code to evaluate the performance using the Environment class.
int startTime, endTime;
startTime = Environment.TickCount;
objConn
= new SqlConnection(strConnectionString);
strQuery
= "SELECT ID, Name FROM Employee";
objCmd
= new SqlCommand(strQuery,
objConn);
objDA =
new SqlDataAdapter(objCmd);
dsEmployee
= new DataSet();
objDA.Fill(dsEmployee,
"dtEmployee");
drpEmployee.DataSource
= dsEmployee.Tables["dtEmployee"];
drpEmployee.DataTextField
= "Name";
drpEmployee.DataValueField
= "ID";
drpEmployee.DataBind();
endTime = Environment.TickCount;
double executionTime
= (double)(endTime - startTime);
lblStart.Text = "START :
" + startTime.ToString();
lblEnd.Text = "END : "
+ endTime.ToString();
lblExecutionTime.Text
= executionTime.ToString();
That’s it we have used the Environment class to measure the time taken to load a DropDownList with 1000 items.
No comments:
Post a Comment