How to measure how long a task takes

If you want to compare the performance of two processes, you can use this code to measure and print out the time takes by each process.

long startTime = DateTime.Now.Ticks;
SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["con"]);
SqlCommand cmd = new SqlCommand("SELECT * FROM Contributions",con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
do
{
while(dr.Read())
{
    SqlInt32 id = dr.GetSqlInt32(0);
    SqlString name = dr.GetSqlString(1);
    SqlMoney dueAmount = dr.GetSqlMoney(2);
    SqlDateTime dueDate = dr.GetSqlDateTime(3);
    Response.Write(id.ToString() + ": " + dueAmount.ToString() + ": " + name.ToString() + ": " + ((DateTime)dueDate).ToString("d") + "<br>");
}
} while (dr.NextResult());
int y=0;
for(int x=0;x<=1000000000;x++)
{
    y = y + 1;
}
long endTime = DateTime.Now.Ticks;
TimeSpan timeTaken = new TimeSpan(endTime - startTime);
Response.Write(timeTaken.ToString() + "<br>");

You might also like...

Comments

Edward Tanguay Edward Tanguay updates his personal web site tanguay.info weekly with code, links, quotes and thoughts on web development. Sign up for the free newsletter.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“The generation of random numbers is too important to be left to chance.” - Robert R. Coveyou