Questions tagged [stopwatch]

The System.Diagnostics.Stopwatch class in .Net, used to accurately measure elapsed time.

837 questions
692
votes
8 answers

Calculate the execution time of a method

Possible Duplicate: How do I measure how long a function is running? I have an I/O time-taking method which copies data from a location to another. What's the best and most real way of calculating the execution time? Thread? Timer? Stopwatch? Any…
Mahdi Tahsildari
  • 13,065
  • 14
  • 55
  • 94
142
votes
18 answers

Is there a stopwatch in Java?

Is there a stopwatch in Java? On Google I only found code of stopwatches that don't work - they always return 0 milliseconds. This code I found doesn't work and I don't see why. public class StopWatch { private long startTime = 0; private…
user1007522
  • 7,858
  • 17
  • 69
  • 113
122
votes
4 answers

Should I Stop Stopwatch at the end of the method?

Let's imagine we have simple measurements using Stopwatch public void DoWork() { var timer = Stopwatch.StartNew(); // some hard work Logger.Log("Time elapsed: {0}", timer.Elapsed); timer.Stop(); // Do I need to call…
Dariusz
  • 15,573
  • 9
  • 52
  • 68
111
votes
20 answers

Converting Milliseconds to Minutes and Seconds?

I have looked through previous questions, but none had the answer I was looking for. How do I convert milliseconds from a StopWatch method to Minutes and Seconds? I have: watch.start(); to start the stopwatch and watch.stop(); to stop the…
user2426902
106
votes
3 answers

Stopwatch vs. using System.DateTime.Now for timing events

I wanted to track the performance of my code so I stored the start and end time using System.DateTime.Now. I took the difference between the two as the time my code to execute. I noticed though that the difference didn't appear to be accurate. So I…
Randy Minder
  • 47,200
  • 49
  • 204
  • 358
79
votes
7 answers

Can Stopwatch be used in production code?

I need an accurate timer, and DateTime.Now seems not accurate enough. From the descriptions I read, System.Diagnostics.Stopwatch seems to be exactly what I want. But I have a phobia. I'm nervous about using anything from System.Diagnostics in…
Adrian
  • 793
  • 1
  • 5
  • 5
66
votes
7 answers

How show minutes and seconds with Stopwatch()

I need to show also the minutes, actually I use this code for show the seconds, but also need the minutes TimeSpan ts = stopwatch.Elapsed; Console.WriteLine("File Generated: " + _writer.getBinaryFileName(filePath, Convert.ToInt32(logSelected)) + "…
ale
  • 3,301
  • 10
  • 40
  • 48
65
votes
5 answers

.NET Stopwatch - performance penalty

Possible Duplicates: Is DateTime.Now the best way to measure a function's performance? Stopwatch vs. using System.DateTime.Now for timing events I have code which needs to run as fast as possible. To be able to log the execution time, I use the…
Ahmet Altun
  • 3,910
  • 9
  • 39
  • 64
59
votes
11 answers

How do I measure how long a function is running?

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…
sebastian.roibu
  • 2,579
  • 7
  • 37
  • 59
55
votes
3 answers

Using Stopwatch in C#

Does anyone know how I can add a timer function to this code that solves sudoku puzzles? I've tried the Stopwatch class but I can't get it to work because I don't know exactly where to put it. I've tried several places but always get errors. The…
PRO_grammer
  • 575
  • 1
  • 4
  • 6
46
votes
4 answers

Difference between ElapsedTicks, ElapsedMilliseconds, Elapsed.Milliseconds and Elapsed.TotalMilliseconds? (C#)

I'm totally confused between these 4. What is the difference between ElapsedMilliseconds (long), ElapsedTicks (long), Elapsed.TotalMilliseconds (double) and Elapsed.Milliseconds (int)? I have a function { Stopwatch sw = new…
nawfal
  • 70,104
  • 56
  • 326
  • 368
39
votes
3 answers

High-Performance Timer vs StopWatch

Does anyone know if the HiPerfTimer or the StopWatch class is better for benchmarking, and why?
Kane
  • 16,471
  • 11
  • 61
  • 86
36
votes
1 answer

Should I call Stop before reading ElapsedMilliseconds?

Can I get the elapsed time since I have called Start on a stopwatch using ElapsedMilliseconds without calling Stop? I have searched a lot round the internet but only saw examples where ElapsedMilliseconds is called after Stop. Is this value filled…
Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
31
votes
8 answers

How accurate is System.Diagnostics.Stopwatch?

How accurate is System.Diagnostics.Stopwatch? I am trying to do some metrics for different code paths and I need it to be exact. Should I be using stopwatch or is there another solution that is more accurate. I have been told that sometimes…
leora
  • 188,729
  • 360
  • 878
  • 1,366
31
votes
12 answers

Display a countdown for the python sleep function

I am using time.sleep(10) in my program. Can display the countdown in the shell when I run my program? >>>run_my_program() tasks done, now sleeping for 10 seconds and then I want it to do 10,9,8,7.... is this possible?
user1681664
  • 1,771
  • 8
  • 28
  • 53
1
2 3
55 56