How do I measure how long a function is running?
Solution 1
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 e)
{
counter++;
btnTabuSearch.Text = counter.ToString();
}
But it's the wrong aproach. You must use the Stopwatch (System.Diagnostic) class because it's a High resolution timer and the word Diagnostic says everything.
So try this:
Stopwatch timer = Stopwatch.StartNew();
Result result = new Result();
result = new GeneticAlgorithms().TabuSearch(parametersTabu, functia);
timer.Stop();
TimeSpan timespan = timer.Elapsed;
btnTabuSearch.Text = String.Format("{0:00}:{1:00}:{2:00}", timespan.Minutes, timespan.Seconds, timespan.Milliseconds / 10);
Solution 2
Don't use a timer - use the Stopwatch
class.
var sw = new Stopwatch();
Result result = new Result();
sw.Start();
result = new GeneticAlgorithms().TabuSearch(parametersTabu, functia);
sw.Stop();
// sw.Elapsed tells you how much time passed
Solution 3
Visual Studio 2015, 2017 Professional and Enterprise has diagnostic tool. If you have a breakpoint at the end of your function, it will display the time and duration under the Events tab as shown in the image:
You can see the article Diagnostic Tools debugger window in Visual Studio 2015 for more details.
PS; So far only Xamarin forms, cross platform project doesnt support this feature. I wish that Microsoft brings this great feature for xamarin forms projects as well.
Solution 4
The best way to see how long a bit of code takes to run is to use System.Diagnostics.Stopwatch
.
Here's an example:
using System.Diagnostics;
#if DEBUG
Stopwatch timer = new Stopwatch();
timer.Start();
#endif
//Long running code
#if DEBUG
timer.Stop();
Debug.WriteLine("Time Taken: " + timer.Elapsed.TotalMilliseconds.ToString("#,##0.00 'milliseconds'"));
#endif
I use the #if DEBUG
to ensure the stopwatch code doesn't get into the production release, but you could do without it.
Solution 5
You should use the stopwatch class for timing functions.
Try the following:
private int counter = 0;
// setup stopwatch and begin timing
var timer = System.Diagnostics.Stopwatch.StartNew();
Result result = new Result();
result = new GeneticAlgorithms().TabuSearch(parametersTabu, functia);
// stop timer and get elapsed time
timer.Stop();
var elapsed = timer.Elapsed;
// display result time
MessageBox.Show(elapsed.ToString("mm':'ss':'fff"));
sebastian.roibu
Updated on April 27, 2021Comments
-
sebastian.roibu about 3 years
I want to see how long a function is running. So I added a timer object on my form, and I came out with this code:
private int counter = 0; // Inside button click I have: timer = new Timer(); timer.Tick += new EventHandler(timer_Tick); timer.Start(); Result result = new Result(); result = new GeneticAlgorithms().TabuSearch(parametersTabu, functia); timer.Stop();
And:
private void timer_Tick(object sender, EventArgs e) { counter++; btnTabuSearch.Text = counter.ToString(); }
But this is not counting anything. Why?
-
Oliver about 12 yearsWouldn't it be easier to print the time with
ts.ToString("hh:mm:ss.ff")
orts.ToString("G")
? -
Matt Burland about 12 yearsNote: with the timer set with an interval of 1ms, you should be careful not to think that your counter is actually the number of milliseconds. Depending on hardware, the minimum interval for a timer (on my computer) is around 15ms. So even if you set the interval to 1ms, it'll only fire every 15ms or so.
-
sebastian.roibu about 12 yearsi will try the timer tomorrow. I just want a counter to see how much time passed since i pressed the button.
-
Ian Mercer over 11 years
var timer = Stopwatch.StartNew();
<-- one less line of code -
Ian Mercer over 11 yearsOr even
var sw = Stopwatch.StartNew();
<-- one less line of code -
dwp4ge about 8 yearsthis has been a long time coming
-
sebastian.roibu over 7 yearsinteresting idea. Thanks for posting.
-
Sebastian Xawery Wiśniowiecki over 7 yearsThe answaer is not correct.
Timer()
code does not work anymore. -
mkb over 6 yearsdon't you think the code contains too much unrelated info, I believe only the helper method and its usage in one line is enough
-
Sumit Deshpande over 6 yearsSome extend i agree but information is not unrelated if one wants to use this concept at broader/enterprise level then he has to consider all this information like logger and all. So I just mentioned detailed way of implementation.