Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 14,652 times

Related Categories

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>");

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.

Comments

  • Re: [3916] How to measure how long a task takes

    Posted by chandra219 on 17 Feb 2007

    Find elapsed time                             'Label lblS...

  • some comments

    Posted by thaind on 04 May 2004

    it work fine, but do you know that are there any unit smaller ticks in C#.
    In C#, each tick = 100 nanosecond (10e-8 s), but i want measure the complexity of some string matching algorithm (i.e Boyer...