Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

How to measure execution time with C#

, 12 Dec 2011
Rate this:
Please Sign up or sign in to vote.
Code that measures a time interval as you would measure the execution time of a task

This C# code measures a time interval as you would measure the execution time of a task:

DateTimestartTime=DateTime.Now;
Console.WriteLine("Started:{0",startTime);
//Executethetasktobetimed
for(int i=1;i<100000;i++)
{
    //Executethetasktobetimed
}
DateTime stopTime = DateTime.Now;
Console.WriteLine("Stopped:{0",stopTime);
TimeSpanelapsedTime=stopTime-startTime;
Console.WriteLine("Elapsed:{0",elapsedTime);
Console.WriteLine("inhours:"+elapsedTime.TotalHours);
Console.WriteLine("inminutes:"+elapsedTime.TotalMinutes);
Console.WriteLine("inseconds:"+elapsedTime.TotalSeconds);
Console.WriteLine("inmilliseconds:"+elapsedTime.TotalMilliseconds);

In C# 2.0, the same thing can be done as follows:

Stopwatchwatch=newStopwatch();
watch.Start();
for(inti=1;i<1000000;i++)
{
    //Executethetasktobetimed
}
watch.Stop();
Console.WriteLine("Elapsed:{0",watch.Elapsed);
Console.WriteLine("Inmilliseconds:{0",watch.ElapsedMilliseconds);
Console.WriteLine("Intimerticks:{0",watch.ElapsedTicks);

For DateTime formatting options, refer to Format date and time.

 
alternatively you may also try http://msdn.microsoft.com/en-us/library/ff650674.aspx[^]

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Denno.Secqtinstien
Technical Lead Erls Corporation
India India
I am Dinesh kumar Choudhary by Name, a software Designer and Developer by Work, a Indian Hindu by Religion, Co-Founder of DSFoundation located at http://dsfoundation.wordpress.com by profession, a Loving husband and a Caring Father by Relation.
 
I have a blog Website at http://dennosecqtinstien.wordpress.com. DSFoundation is the Autonomous body to serve for the management of Local shops located at New Delhi, India. Now a days DSFoundation and its subsidiaries are managed by Erls Corporation, an Another initiative by me and my colleagues, in which i am the another CO-Founder of the Organization. Erls Corporation can be located at http:erlsindia.co.in
Follow on   Twitter

Comments and Discussions

 
GeneralReason for my vote of 1 http://msdn.microsoft.com/en-us/libr... PinmemberSangunni12-Dec-11 22:02 
Generalyes copied but was very much help full for me in beginning s... PinmemberDenno.Secqtinstien12-Dec-11 18:23 
GeneralReason for my vote of 1 might as well be a tip on how to dec... Pinmembercechode12-Dec-11 13:50 
GeneralReason for my vote of 1 another plagiarism http://en.csharp-... PinmemberSelvin12-Dec-11 6:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140922.1 | Last Updated 13 Dec 2011
Article Copyright 2011 by Denno.Secqtinstien
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid