How do I measure how long a function is running?

To avoid future problems with a timer, here is the right code: timer = new Timer(); timer.Tick += new EventHandler(timer_Tick); timer.Interval = 1; //set interval on 1 milliseconds timer.Enabled = true; //start the timer Result result = new Result(); result = new GeneticAlgorithms().TabuSearch(parametersTabu, functia); timer.Enabled = false; //stop the timer private void timer_Tick(object sender, EventArgs … Read more

Raise event in high resolution interval/timer

Using a multimedia timer should give you about 1000 events per second. This code should help you on the way. public delegate void TimerEventHandler(UInt32 id, UInt32 msg, ref UInt32 userCtx, UInt32 rsv1, UInt32 rsv2); /// <summary> /// A multi media timer with millisecond precision /// </summary> /// <param name=”msDelay”>One event every msDelay milliseconds</param> /// <param … Read more

Stopwatch vs. using System.DateTime.Now for timing events [duplicate]

As per MSDN: The Stopwatch measures elapsed time by counting timer ticks in the underlying timer mechanism. If the installed hardware and operating system support a high-resolution performance counter, then the Stopwatch class uses that counter to measure elapsed time. Otherwise, the Stopwatch class uses the system timer to measure elapsed time. Use the Frequency … Read more

Calculate the execution time of a method

Stopwatch is designed for this purpose and is one of the best ways to measure time execution in .NET. var watch = System.Diagnostics.Stopwatch.StartNew(); // the code that you want to measure comes here watch.Stop(); var elapsedMs = watch.ElapsedMilliseconds; Do not use DateTime to measure time execution in .NET. UPDATE: As pointed out by @series0ne in … Read more