
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
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
//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;
}-Offline- |