3

This is my first question on stackoverflow.com, so please, be lenient for me :) I have a problem about how do I theoretically create a stopwatch program. It doesn't matter for me which language it would be written in. Is this theoretically OK?

a = 0
While 1 == 1:
    Print(a)
    Sleep(1s)
    a = a+1

This for my eye should be OK, but when running similar thing in Python, it doesn't print it accurately. Measuring it with my "real-life" analog stopwatch, it sometimes is less than 1 second, and sometimes more than 1 second. What i would like to know is how to only theoretically create a stopwatch, I don't need an actual code. I hope you understand my question, awaiting reply :)

mbgfa
  • 78
  • 8
  • Look at using a Timer object. http://docs.python.org/release/2.5.2/lib/timer-objects.html – jac Aug 23 '12 at 19:22
  • I've made this: `import datetime now = datetime.datetime.now() while 1 == 1: print(now.second)` My question is: Why it prints only one value? In my output I can see: `12 12 12 12 12 12 12 12 12 12 12` I know it's a little bit out of topic, but it's not worth opening a new question. – mbgfa Aug 23 '12 at 20:09
  • Because you only set your now variable once, before you entered your loop. You need to update the now variable so you would need to set it inside your loop. – jac Aug 23 '12 at 21:51

3 Answers3

4

Check this out: Stopwatch In Python

So, to answer your question: Yes, it is possible.

As for the accuracy of your original code: How accurate is python's time.sleep()?

Community
  • 1
  • 1
Charlie G
  • 814
  • 9
  • 22
  • Thanks, so if "For non-realtime OS's like a stock Windows the smallest interval you can sleep for is about 10-13ms.", how do I create an accurate stopwatch counting in thousand parts of seconds? For example, 0.001 0.002? – mbgfa Aug 23 '12 at 19:24
  • Here is another example and some discussion about resolution, http://stackoverflow.com/questions/1938048/high-precision-clock-in-python – jac Aug 23 '12 at 19:35
  • @MichałDurak If the smallest interval is 10-13ms, I'm not sure you can actually count by ms. – Charlie G Aug 23 '12 at 19:48
2

sleep, is not guaranteed to sleep for the exact amount of time given; it will sleep for approximately that amount of time, but it may be a little off. Of course, if it is a little off in the same direction every time, that will start to accumulate, and your answer will eventually be very off.

Luckily, your computer comes in with a clock that doesn't drift very fast, and you can use that as a stopwatch. Instead of sleeping for one second each time, sleep for a shorter period (such as a hundredth of a second). Each time you wake up, test the current time. If one more second has elapsed since your start time, print it.

Brian Campbell
  • 322,767
  • 57
  • 360
  • 340
  • So, how is mine computer's clock made, that it doesn't drift that fast? Very complicated software, or rather hardware? – mbgfa Aug 23 '12 at 20:18
  • @Micechal A combination, actually. There's a piece of hardware known as a [real-time clock](http://en.wikipedia.org/wiki/Real-time_clock) that keeps track of the time. This doesn't drift very fast; it's basically equivalent to a digital watch, with a crystal oscillator, and it has a battery too to keep time even while your computer is off. However, it will drift eventually, so the [network time protocol, NTP](http://en.wikipedia.org/wiki/Network_Time_Protocol), is used to keep it in sync with more accurate clocks, such as the atomic clocks used to set the official time standards. – Brian Campbell Aug 24 '12 at 20:06
0

The reason why printing happens in a non-regular way may be that the output is buffering. If you disable buffering for standard output, it should work as expected; see Disable output buffering.

When running an existing script, you can achieve that by providing the -u option to python (at least on Linux). From man python:

-u Force stdin, stdout and stderr to be totally unbuffered. On systems where it matters, also put stdin, stdout and stderr in binary mode. Note that there is internal buffering in xreadlines(), readlines() and file-object iterators ("for line in sys.stdin") which is not influ‐ enced by this option. To work around this, you will want to use "sys.stdin.readline()" inside a "while 1:" loop.

Community
  • 1
  • 1
Lev Levitsky
  • 63,701
  • 20
  • 147
  • 175