What's Here?
- Members: 137,214
- Replies: 481,423
- Topics: 75,054
- Snippets: 2,567
- Tutorials: 675
- Total Online: 2,273
- Members: 111
- Guests: 2,162
|
This is a snippet used to measure the execution time of a task or process. It uses the System.Diagnostics.StopWatch to measure the time
|
Submitted By: PsychoCoder
|
|
|
Rating:
|
|
Views: 488 |
Language: C#
|
|
Last Modified: January 2, 2008 |
Instructions: Pass the method the number of times to execute the task or process, results are returned in a HashTable format showing:
1) Total elapsed time
2) Milliseconds elapsed
3) Timer ticks elapsed |
Snippet
//Namespace reference
using System;
using System.Diagnostics;
/// <summary>
/// method to measure the execution time of a
/// task or process.
/// </summary>
/// <param name="num">Number of times to execute the task</param>
/// <returns></returns>
public Hashtable MeasureExecutionTime(int num)
{
//HashTable instance to hold execution times
Hashtable time = new Hashtable ();
Stopwatch timer = new Stopwatch ();
try
{
//start your StopWatch
timer.Start();
for (int i = 1; i < num; i++)
{
//execute your process or task to be times
}
//stop the StopWatch once the processing is complete
timer.Stop();
//Add the values to our HashTable
time.Add("Elapsed: ", timer.Elapsed);
time.Add("In milliseconds: ", timer.ElapsedMilliseconds);
time.Add("In timer ticks: ", timer.ElapsedTicks);
}
catch (Exception ex)
{
time = null;
MessageBox.Show(ex.Message);
}
return time;
}
Copy & Paste
|
|
|
Reference Sheets
Bye Bye Ads
Monthly Drawing
Top Contributors
Top 10 Kudos This Month
|