You can mesure the time using date variables where you can set the first variable to DateTime.Now and the second also the same, and then substract those dates and get passed time for executing method. There is also another way, to mesure CPU ticks needed to execute your method. Here is a class for that:
public class PerfTimer
{
[DllImport("Kernel32.dll")]
public static extern void QueryPerformanceCounter(ref long ticks);
private long startTime = 0;
private long stopTime = 0;
public void Start()
{
QueryPerformanceCounter(ref startTime);
}
public void Stop()
{
QueryPerformanceCounter(ref stopTime);
}
public long Time
{
get { return stopTime - startTime; }
}
}
You will invoke start method before the start of the method and stop after finishing method. And then you will have CPU ticks needed to finish the method. Here is an exaple how to do that:
Int64 ticks = 0;
perfTimer.Start();
YourMethod(); //execute your method
perfTimer.Stop();
ticks = perfTimer.Time;
Debug.WriteLine("ticks: " + ticks);
QueryPerformanceCounter uses CPU clock timer and that's way is more accurate. But be aware that doesn't works properly on all processors, for example Athlon64.