Click here to Skip to main content
12,360,037 members (61,624 online)
Click here to Skip to main content
Add your own
alternative version

Stats

5K views
1 bookmarked
Posted

How To: Measure execution time in C#

, 9 Mar 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
Please try this method to get execution time. I'm not sure that it is very accurate.public static TimeSpan GetDuration(Stopwatch sw, ThreadStart method){ sw.Reset(); sw.Start(); method(); sw.Stop(); return sw.Elapsed;}you can put it into any common static class and use as...
Please try this method to get execution time. I'm not sure that it is very accurate.

public static TimeSpan GetDuration(Stopwatch sw, ThreadStart method)
{
	sw.Reset();
	sw.Start();
	method();
	sw.Stop();
	return sw.Elapsed;
}

you can put it into any common static class and use as follows:

IList dataObjects = GetObjects(); // execution time should be tested

foreach (DataObject do in dataObject)
{
}

Execution time can be measured in such way:

IList dataObjects = null;
Stopwatch sw = new Stopwatch();
Console.WriteLine("GetObjects executed in {0}", GetDuration(sw, delegate() {
 
dataObjects = GetObjects();
 
}));
foreach (DataObject do in dataObject)

License

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

Share

About the Author

spot9969
Germany Germany
No Biography provided

You may also be interested in...

Comments and Discussions

 
SuggestionYou can get more precise measurements! Pin
Pavel Vladov11-Jul-12 22:12
memberPavel Vladov11-Jul-12 22:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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 | Terms of Use | Mobile
Web02 | 2.8.160621.1 | Last Updated 9 Mar 2011
Article Copyright 2011 by spot9969
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid