473,395 Members | 1,335 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

measuring 1/100th seconds, what function?

Hello.

I'm doing a Rubik's Cube timer, so I need a function to measure 100ths of
a second. I've browsed through the library reference of python.org, but I
didn't find anything that struck me as 'the perfect fit'. Either it was
system dependent, or it was too grainy (lo-res), or... something else.

so, I need a function which satisfies (in order of importance):

1. It can be used to measure time in the range of up to approximately 1
minute with an accuracy down to 100th-seconds (that's 60000~65536
different values). Better accuracy is a bonus, longer time-frame is a
bigger bonus.

2. it must run on any version of windows upon which python 2.4 can run
(target systems), and run on my system (debian/sarge) __ without the need
for changing the code dependent of environment; any supported
non-Linux/non-windows sytem is a bonus, other supported Linuxes (sp?) is a
bigger bonus (however, I don't expect that it will _not_ work dependent of
distroes)

I'm sorry for asking about such a triviality, but hey, if I can't get it
by RTFM, TFM is buggy ;)

Jonas Kölker

Jul 18 '05 #1
8 1699
Jonas Kölker wrote:
I'm doing a Rubik's Cube timer, so I need a function to measure 100ths of
a second. I've browsed through the library reference of python.org, but I
didn't find anything that struck me as 'the perfect fit'. Either it was
system dependent, or it was too grainy (lo-res), or... something else.
It usually helps to specify what you looked at, and perhaps why you
didn't think it was acceptable, to avoid us retracing your steps...
so, I need a function which satisfies (in order of importance):

1. It can be used to measure time in the range of up to approximately 1
minute with an accuracy down to 100th-seconds (that's 60000~65536
different values). Better accuracy is a bonus, longer time-frame is a
bigger bonus.

2. it must run on any version of windows upon which python 2.4 can run


These two constraints cannot be met simultaneously, I believe.
Windows98 does not provide adequate resolution in the timer.

If you are willing to forego Win98 and use only the real Windows OSes,
then I think you get roughly (?) 10ms resolution already with
time.time(). Linux probably gives 1ms resolution or better in pretty
much all cases with time.time().

-Peter
Jul 18 '05 #2
On 2004-08-10, Jonas Kölker <jo**********@yahoo.com> wrote:
I'm sorry for asking about such a triviality, but hey, if I
can't get it by RTFM, TFM is buggy ;)


Remember to submit a patch when you figure out how to fix the
bug.

--
Grant Edwards grante Yow! .. One FISHWICH
at coming up!!
visi.com
Jul 18 '05 #3
[Jonas Kölker]
I'm doing a Rubik's Cube timer, so I need a function to measure
100ths of a second. ...


Assuming you want to compute wall-time deltas (as opposed to CPU-time
deltas), about the best you can do x-platform is

if sys.platform == "win32":
from time import clock as now
else:
from time import time as now

Then use now(). Unsure which branch Cygwin should take. It's up to
you to determine whether it works "well enough" on each platform you
care about. Note that on a box connected to a network time-correction
service, time.time can appear to "run backwards" briefly at
unpredictable times. The same kind of thing can happen on multi-CPU
boxes -- or not, depending on piles of platform details.
Jul 18 '05 #4
On Tue, 10 Aug 2004 17:30:14 -0400, Peter Hansen <pe***@engcorp.com> wrote:
Jonas Kölker wrote:
I'm doing a Rubik's Cube timer, so I need a function to measure 100ths of
a second. I've browsed through the library reference of python.org, but I
didn't find anything that struck me as 'the perfect fit'. Either it was
system dependent, or it was too grainy (lo-res), or... something else.


It usually helps to specify what you looked at, and perhaps why you
didn't think it was acceptable, to avoid us retracing your steps...
so, I need a function which satisfies (in order of importance):

1. It can be used to measure time in the range of up to approximately 1
minute with an accuracy down to 100th-seconds (that's 60000~65536
different values). Better accuracy is a bonus, longer time-frame is a
bigger bonus.

2. it must run on any version of windows upon which python 2.4 can run


These two constraints cannot be met simultaneously, I believe.
Windows98 does not provide adequate resolution in the timer.

If you are willing to forego Win98 and use only the real Windows OSes,
then I think you get roughly (?) 10ms resolution already with
time.time(). Linux probably gives 1ms resolution or better in pretty
much all cases with time.time().

On windows I like to use time.clock()
For NT4 with python 2.3.2 (yeah I ought to upgrade both ;-)
from time import time,clock
min(filter(None,[-(time()-time()) for i in xrange(100000)])) 0.0099999904632568359 min(filter(None,[-(clock()-clock()) for i in xrange(100000)])) 5.8666657594130811e-006

IOW, time apparently gets the NT4 basic os time slice delta (10ms),
whereas clock tries to get something better (and it's pretty good).
The filter call is because time()-time() makes a lot of zeroes.
clock() is fast enough not to require it. Not one zero in 100k, anyway. min([-(clock()-clock()) for i in xrange(100000)])

5.8666656741479528e-006

And that's not the minimum resolution of the timer clock() uses, it's
near the minimum time between two readings you can get from python.
Maybe some other loop mechanism can get two two samples faster and
then subtract them. A game for someone ;-)

Regards,
Bengt Richter
Jul 18 '05 #5
Bengt Richter wrote:
On windows I like to use time.clock() [snip samples showing better resolution than time.time()] IOW, time apparently gets the NT4 basic os time slice delta (10ms),
whereas clock tries to get something better (and it's pretty good).


All true, though the OP said he wanted code that would work on both
platforms (though Tim's response shows how trivial it is to
dynamically pick the best routine).

Presumably there's a human involved somewhere, plus a multitasking
OS with other things going on, and Python as well (not widely known
as a good hard realtime language ;-), so going for the extra
resolution of time.clock() is likely to be an exercise in higher
resolution, but no better accuracy...

-Peter
Jul 18 '05 #6
Tim Peters <ti********@gmail.com> wrote:
Note that on a box connected to a network time-correction
service, time.time can appear to "run backwards" briefly at
unpredictable times.


This will never happen if you're running NTP. NTP does gradual
adjustments to the clock rate to ensure that the system clock is always
monotonically increasing.

It can happen if you're running some kind of time sync service other
than NTP. Why anybody would want to do such a thing is beyond me.
Jul 18 '05 #7
Roy Smith <ro*@panix.com> writes:
Tim Peters <ti********@gmail.com> wrote:
Note that on a box connected to a network time-correction
service, time.time can appear to "run backwards" briefly at
unpredictable times.


This will never happen if you're running NTP. NTP does gradual
adjustments to the clock rate to ensure that the system clock is always
monotonically increasing.


ntp only works well if your clock rate is very consistent. On all of
the laptops I've used, I get frequent adjustments:

Aug 6 20:30:45 localhost ntpd[911]: time reset -0.472600 s
Aug 5 22:04:57 localhost ntpd[919]: time reset +0.670974 s
Aug 5 22:34:06 localhost ntpd[919]: time reset +0.884625 s
Aug 5 23:13:54 localhost ntpd[919]: time reset -0.376758 s

I don't see any reason in principle ntp couldn't allow a greater
offset between the local machine and the servers, and use gradual
adjustments in this case. But it doesn't.

Dan
Jul 18 '05 #8
In article <87************@uwo.ca>, Dan Christensen <jd*@uwo.ca> wrote:
Roy Smith <ro*@panix.com> writes:
Tim Peters <ti********@gmail.com> wrote:
Note that on a box connected to a network time-correction
service, time.time can appear to "run backwards" briefly at
unpredictable times.


This will never happen if you're running NTP. NTP does gradual
adjustments to the clock rate to ensure that the system clock is always
monotonically increasing.


ntp only works well if your clock rate is very consistent. On all of
the laptops I've used, I get frequent adjustments:

Aug 6 20:30:45 localhost ntpd[911]: time reset -0.472600 s
Aug 5 22:04:57 localhost ntpd[919]: time reset +0.670974 s
Aug 5 22:34:06 localhost ntpd[919]: time reset +0.884625 s
Aug 5 23:13:54 localhost ntpd[919]: time reset -0.376758 s

I don't see any reason in principle ntp couldn't allow a greater
offset between the local machine and the servers, and use gradual
adjustments in this case. But it doesn't.

Dan


I stand corrected. Thank you.
Jul 18 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: NotGiven | last post by:
Below is a good elapsed time function I found. However, I'd like to return total seconds instead of broken down into days, hours, minutes & seconds. In other words, I want "125" instead of "2...
6
by: Kription | last post by:
Hello, I am having a nightmare of a time getting seconds converted to hour-minute-seconds I have a third party application that outputs only in seconds since so if someone has been logged...
6
by: Mudge | last post by:
I'm writing PHP Web sites that are database driven with mysql. I'm looking for ways to measure the performance (speed) of my php code, so that I can find ways to write the fastest php code. Any...
1
by: Carlo Filippini | last post by:
Hi I am trying to measure the perofrmance of a wireless link using maily ftp. Unfortunately get and put from Net::FTP do not give me an measure of the performances. I am sure somebody must have...
74
by: Dominik Wallner | last post by:
Hi! I'm currently implementing a program which measures voltages through an external USB-AD-converter. It should output those values as time/voltage pairs. My problem is to measure the time...
20
by: upperclass | last post by:
Hi, I'm trying to find a decent way of measuring the running time of a long-running program. Say, the running time ranges from several seconds to more than a day. The standard clock()...
2
by: upperclass | last post by:
Hi, I'm trying to find a decent way to measure program running time. I know clock() is probably the standard way of doing it but clock_t overflows too quickly. The target program running time...
7
by: i12know | last post by:
Hi guys, I have tried a lot of code and read forums discussing about C or C++ functions that measures time in microsecond, but there still seems to be confusions about that. Can anyone tell me...
9
by: Ross | last post by:
I'm a newbie at this, and have searched a lot but can't find something that seems appropriate for measuring a recurring elapsed time. Creating an object with: var mydate = new Date(); seems...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.