473,403 Members | 2,222 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,403 software developers and data experts.

Determining actual elapsed (wall-clock) time

I would like to determine the "actual" elapsed time of an operation
which could take place during a time change, in a platform-independent
manner (at least across Linux/Windows machines).

Using time.time() doesn't appear to be suitable, since time might jump
forwards or backwards at the user's whim, if the system clock is reset,
or when a daylight savings time change occurs.

Using time.clock() doesn't appear to be suitable on Linux, though it
appears sufficient on Windows. (I'm willing to assume nobody will reset
whatever QueryPerformanceCounter() uses to return the time.clock() value
on Windows.)

There used to be a "timing" module (according to
http://effbot.org/librarybook/timing.htm) which would likely have worked
(for Unix at least), but it's not in the latest Pythons.

I can deal with wraparounds if necessary. I'm not too concerned about
the resolution, though better than one second would be useful.

Thanks for any suggestions.

-Peter
Jul 19 '05 #1
13 3259
Peter Hansen <pe***@engcorp.com> wrote:
I would like to determine the "actual" elapsed time of an operation
which could take place during a time change, in a platform-independent
manner (at least across Linux/Windows machines).

Using time.time() doesn't appear to be suitable, since time might jump
forwards or backwards at the user's whim, if the system clock is reset,
or when a daylight savings time change occurs.


If you get the UTC time, daylight savings time doesn't enter the equation.
If the system clock is reset, however, you're out of luck. I can't think
of any time-related API which doesn't rely on the system clock as a
reference. If the system clock is good, you get good time. If the system
clock sucks, or changes, you don't.

If you care about time, you want your system clock controlled by NTP.
There's just no excuse not to.

Is there some reason you can't just use the system clock? I suppose if you
had to, you could hobble together your own NTP client which keeps network
time independent of the system clock. But that would be a lot of work and
it's hard to imagine the effort would be justified.
Jul 19 '05 #2
Roy Smith wrote:
Peter Hansen <pe***@engcorp.com> wrote:
If you get the UTC time, daylight savings time doesn't enter the equation.
Of course... I didn't think of that approach. I don't actually care
about absolute time, so this should work fine for at least the DST case.
If you care about time, you want your system clock controlled by NTP.
There's just no excuse not to.
I guess as long as the NTP client is set up to ensure the time
adjustments are smaller than some value X, it would be acceptable. I'll
have to look into how to set up Windows XP to prevent users from
changing the time on their own, assuming that's possible.
Is there some reason you can't just use the system clock?


I was anticipating use of the system (running on Windows in this case)
by users who might decide to change the time, and in certain cases that
could cripple the software. I hadn't thought of the possibility of
simply preventing that, though if I insist on an NTP client being
installed, they shouldn't have a problem with that.

Thanks for correcting my thought process...

-Peter
Jul 19 '05 #3
Peter Hansen <pe***@engcorp.com> wrote:
I guess as long as the NTP client is set up to ensure the time
adjustments are smaller than some value X, it would be acceptable.
NTP is generally capable of keeping the various system clocks on a LAN
within a few ms of each other, and within a few 10's of ms over the
Internet from GPS, WWV, or similar international time references.
I'll have to look into how to set up Windows XP to prevent users from
changing the time on their own, assuming that's possible.


On a single-user system like Windows, you pretty much have to assume the
user can do anything. They can turn off NTP, reset the clock, reboot the
system, uninstall your software, whatever.

If you could check to see that NTP is running, it doesn't prove anything.
A malicious and determined user could set up another machine as a NTP
server to synch against, and even configure that machine to look like it
was a stratum-1 reference (i.e. an atomic clock).

At some point, you need to decide if you trust the system administrator to
supply you with an accurate system clock or not. If you don't, and it's
really important that you have an accurate time reference, you've got an
interesting engineering problem on your hands.
Jul 19 '05 #4
Roy Smith wrote:
Peter Hansen <pe***@engcorp.com> wrote:
I'll have to look into how to set up Windows XP to prevent users from
changing the time on their own, assuming that's possible.


On a single-user system like Windows, you pretty much have to assume the
user can do anything. They can turn off NTP, reset the clock, reboot the
system, uninstall your software, whatever.


Actually, I suspect that a non-administrator user can be restricted from
doing several of those things under XP, which would simplify things.

And, in any case, I'm not remotely concerned about malicious users, just
"fiddlers" who might think their watch time is somehow more correct than
what the computer says...

-Peter
Jul 19 '05 #5
Roy Smith wrote:
Peter Hansen <pe***@engcorp.com> wrote:
Using time.time() doesn't appear to be suitable, since time might jump
forwards or backwards at the user's whim, if the system clock is reset,
or when a daylight savings time change occurs.


If you get the UTC time, daylight savings time doesn't enter the equation.


Hmmm... not only that, but at least under XP the return value of
time.time() _is_ UTC. At least, it's entirely unaffected by the
daylight savings time change, or (apparently) by changes in time zone.

Looks like time.time() might be sufficient. (That might be what Roy
intended, but I initially thought he meant time.gmtime(time.time()),
which could have resulted in a race condition if time.time() jumped
ahead or back separately from some internal daylight savings time flag,
and the value was read between the two operations.)

-Peter
Jul 19 '05 #6
[Peter Hansen]
Hmmm... not only that, but at least under XP the return value of
time.time() _is_ UTC. At least, it's entirely unaffected by the
daylight savings time change, or (apparently) by changes in time zone.


On all platforms, time.time() returns the number of seconds "since the
epoch". All POSIX systems agree on when "the epoch" began, but that
doesn't really matter to your use case. Number of seconds since the
epoch is insensitive to daylight time, time zone, leap seconds, etc.
Users can nevertheless make it appear to jump (into the future or the
past) by changing their system clock. If you need an absolute measure
of time immune to user whims, you need to connect to special hardware,
or to an external time source.
Jul 19 '05 #7
Roy Smith wrote:
Peter Hansen <pe***@engcorp.com> wrote:
I guess as long as the NTP client is set up to ensure the time
adjustments are smaller than some value X, it would be acceptable.

NTP is generally capable of keeping the various system clocks on a LAN
within a few ms of each other, and within a few 10's of ms over the
Internet from GPS, WWV, or similar international time references.

I'll have to look into how to set up Windows XP to prevent users from
changing the time on their own, assuming that's possible.

On a single-user system like Windows, you pretty much have to assume the
user can do anything. They can turn off NTP, reset the clock, reboot the
system, uninstall your software, whatever.

If you could check to see that NTP is running, it doesn't prove anything.
A malicious and determined user could set up another machine as a NTP
server to synch against, and even configure that machine to look like it
was a stratum-1 reference (i.e. an atomic clock).

At some point, you need to decide if you trust the system administrator to
supply you with an accurate system clock or not. If you don't, and it's
really important that you have an accurate time reference, you've got an
interesting engineering problem on your hands.


A couple of quick thoughts: (1) stupidity is much more prevalent than
malice in that environment.

(2) Peter, if your app has something else to measure e.g. it is
processing zillions of rows from a database, grab the [UTC] wall time
every N things, and apply plausibility checks to the speed N/delta(wall)
-- if it goes negative or "too high" or "too slow", holler fer a mountie.
Jul 19 '05 #8
On Sat, 02 Jul 2005 18:55:46 -0400, Peter Hansen <pe***@engcorp.com>
declaimed the following in comp.lang.python:

And, in any case, I'm not remotely concerned about malicious users, just
"fiddlers" who might think their watch time is somehow more correct than
what the computer says...
In my case, it often is... I've got four WWVB clocks in my
apartment (one wall powered with LASER/LED projector to the ceiling, one
part of a "weather station" that also reads temp/humidity from three
remotes, a battery alarm clock, and a travel clock), along with two GPS
units, and a DVD-R and VCR that time synch to PBS signals... My watch
tends to be less than 20 seconds off when the 6-month DST change takes
place. My computers tend to be a few minutes off in that time span.

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 19 '05 #9
I've needed to do something similar in the past and used free ntp
servers. Of course you need an internet connection:
http://ntp.isc.org/bin/view/Servers/NTPPoolServers
http://www.nightsong.com/phr/python/setclock.py

You could also have a startup script spawn a thread that stores the
time persistently in a shelf object, sleeps for a minute, and checks to
see if the current clock time is greater than a minute (+/-sec,) and
subtracts the difference. Or even check it against an NTP server if you
can.

Jul 19 '05 #10
On Sat, 2 Jul 2005 19:44:19 -0400, Tim Peters <ti********@gmail.com> wrote:
[Peter Hansen]
Hmmm... not only that, but at least under XP the return value of
time.time() _is_ UTC. At least, it's entirely unaffected by the
daylight savings time change, or (apparently) by changes in time zone.


On all platforms, time.time() returns the number of seconds "since the
epoch". All POSIX systems agree on when "the epoch" began, but that
doesn't really matter to your use case. Number of seconds since the
epoch is insensitive to daylight time, time zone, leap seconds, etc.=20
Users can nevertheless make it appear to jump (into the future or the
past) by changing their system clock. If you need an absolute measure
of time immune to user whims, you need to connect to special hardware,
or to an external time source.


For the latter, Peter, you can probably adapt Paul Rubin' setclock.py
found at
http://www.nightsong.com/phr/python/setclock.py

Regards,
Bengt Richter
Jul 21 '05 #11
In article <3Z********************@powergate.ca>,
Peter Hansen <pe***@engcorp.com> wrote:

I would like to determine the "actual" elapsed time of an operation
which could take place during a time change, in a platform-independent
manner (at least across Linux/Windows machines).

Using time.time() doesn't appear to be suitable, since time might jump
forwards or backwards at the user's whim, if the system clock is reset,
or when a daylight savings time change occurs.


You have to do one of two things: rely on system clock or require a
network connection (so you can call a timeserver directly).
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

f u cn rd ths, u cn gt a gd jb n nx prgrmmng.
Jul 21 '05 #12
The traditional use of gettimeofday() to (insecurely and unreliably)
approximate elapsed local time is one of my pet peeves.

Fortunately a real monotonic clock has finally been added to the linux
kernel and glibc:

http://www.imperialviolet.org/page24.html#e474

If you have a recent enough kernel and glibc you can use that. On
Windows you can use system timers and handle wraparound yourself.

And you can write an abstract Pythonic layer over both and give it to
me. ;-)

Jul 21 '05 #13
zooko wrote:
The traditional use of gettimeofday() to (insecurely and unreliably)
approximate elapsed local time is one of my pet peeves.

Fortunately a real monotonic clock has finally been added to the linux
kernel and glibc:

http://www.imperialviolet.org/page24.html#e474


Interestingly, the author of that page appears to have made a number of
the same misassumptions about the actual behaviour of the timeofday
clock (assuming time.time() fairly faithfully reproduces/wraps that
behaviour). Either that or time.time() does magic that avoids the bulk
of the problems in which case I can say only "sucks not to be using
Python, doesn't it?"

I'd be more interested in this monotonic clock feature if it were not
the case that time.time() is pretty much monotonic except in the face of
the minor (sub-100ms) tweaks that might occur once every week or two
with an NTP client running. It certainly doesn't cause the one hour
jumps forwards and backwards which I and the author of that page both
thought it would.

-Peter
Jul 21 '05 #14

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

Similar topics

1
by: Brett | last post by:
In VB, a UserControl could determine its run-time vs. design-time status by evaluating the UserControl.Ambient.UserMode. For the life of me I can't figure out how to do the same basic thing in...
0
by: Lew | last post by:
Hi, If I want to recover my database to just before a certain transaction occurred how do I do it? How do I determine the exact timestamp to rollforward to. I remember years ago when I worked...
12
by: aegis | last post by:
What is wall clock time? the standard doesn't define it but I see its use in past posts on clc. -- aegis
0
by: Paul Cook | last post by:
Hi I am trying to determine whether a control is being displayed at designtime or runtime but this piece of code doesn't seem to work correctly. Note that this is not a web user control (ascx)...
1
by: Geert Jansen | last post by:
Hi! I'm trying to profile an application that I believe is blocking on I/O for a significant amount of time. In trying to dig down where this happens, I profiled the application with hotshot....
0
by: Anurag | last post by:
Hi, ENV: DB2 ESE 8.2.3 DPF (11 nodes) on AIX 5.x ==== SCENARIO / SETUP ======== ====== (1) I needed to find out CPU Time (User / System) of SQL queries for benchmark testing. (2) I setup...
5
by: pedro.ballester | last post by:
Hi everyone, I am struggling with the following problem. I would like to measure the wall clock time required to run a section of the code with a precision of milliseconds. The attached code...
1
by: Vince | last post by:
Hi, I know this has been asked before but the answers I've seen don't answer my question. When a web surfer visits a site, is there a way to detect what region/ language there PC is setup on?...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.