473,395 Members | 1,456 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.

Anomaly in time.clock()

Hello,

I have been reading a thread about time.clock() going backward, which
is exactly what I am seeing... the thread generally leaning toward the
problem is caused by multi-processor machines. But I am seeing it at a
single CPU computer, and running XP.

The error I am seeing between two very close invokation of
time.clock() is always around 3.5 seconds! This is enough to throw a
lot of the timing requirement in the software out of the window...
this is a fairly serious problem. A quick fix will be to capture the
time.clock() going backward 3.5 seconds, and then adjust rest of the
system accordingly. But I don't feel like this is a good way around
this problem. I was using time.time() before switching to
time.clock(). The reason I changed is because I need a way to
calculate elapsed time independent of system time.

Should I just stick with time.time() instead? Or is there another way
to calculate elapsed time accurately, independent of system time being
changed?
Mar 17 '08 #1
7 1822
Godzilla schreef:
Hello,

I have been reading a thread about time.clock() going backward, which
is exactly what I am seeing... the thread generally leaning toward the
problem is caused by multi-processor machines. But I am seeing it at a
single CPU computer, and running XP.

The error I am seeing between two very close invokation of
time.clock() is always around 3.5 seconds! This is enough to throw a
lot of the timing requirement in the software out of the window...
this is a fairly serious problem.
I don't know if it's the same problem, but I've seen something similar
to this. Are you by any chance using DirectX? When you create a DirectX
device, DirectX by default resets the precision of the CPU to 20 bits or so.

I had that problem in a project at work, with similar results (and other
strange calculation errors). The solution was to use a flag
(PRESERVE_PRECISION or something like that) in the CreateDevice() call
(it was a C++ project).

If it's something else, I'm afraid I can't help.

--
The saddest aspect of life right now is that science gathers knowledge
faster than society gathers wisdom.
-- Isaac Asimov

Roel Schroeven
Mar 17 '08 #2
Godzilla <go***********@gmail.comwrote:
>But the time.clock() sometimes return a value of between -3.5 to -4.5
seconds backward.
There are race conditions in your code. In between the time you execute
"curTime = time.clock()" and calculate "curTime - self.timeStamp" in one
thread, the other thread can execute "self.timeStamp = time.clock()".
It's the only way your example program can print a negative "Actual
Elapsed Time" value. The race condition seems unlikely, and it's hard
to explain how this could result in it printing a value in the range
of -3.5 to -4.5. However, a race condition occuring between the two
evaluations of "curTime - self.timeStamp" is the only way your example
program could print a negative value.

Ross Ridge

--
l/ // Ross Ridge -- The Great HTMU
[oo][oo] rr****@csclub.uwaterloo.ca
-()-/()/ http://www.csclub.uwaterloo.ca/~rridge/
db //
Mar 19 '08 #3
On Mar 19, 11:17 pm, Godzilla <godzillais...@gmail.comwrote:
Hi John,

I am using time.clock to calculate the elapsed time. Below is an
example of what I was trying to do:

import time
import thread
Silly me, not being able to infer that from your initial post!

[snip]
>
But the time.clock() sometimes return a value of between -3.5 to -4.5
seconds backward. Note that not all computers are behaving the same. I
have not experience the same problem with the computer at home.
Same "service pack" number?

Your code "worked" (no time warp) for me on a single-core AMD Turion64
CPU running (32 bit) Windows XP Service Pack 2 Build 2600. Maybe the
ones that "fail" have dual-core CPUs.
Mar 19 '08 #4
Just found out that win32api.GetTickCount() returns a tick count in
milli-second since XP started. Not sure whether that is reliable.
Anyone uses that for calculating elapsed time?
Mar 21 '08 #5
On 21 mar, 03:13, Godzilla <godzillais...@gmail.comwrote:
Just found out that win32api.GetTickCount() returns a tick count in
milli-second since XP started. Not sure whether that is reliable.
Anyone uses that for calculating elapsed time?
I use GetTickCount on other languages because it's easy to call. Note
that it returns a value in ms but the *precision* may be not so high.
Anyway I think your problem is in the code, not on time.clock()

--
Gabriel Genellina
Mar 21 '08 #6
Godzilla <go***********@gmail.comwrote:
>
Just found out that win32api.GetTickCount() returns a tick count in
milli-second since XP started. Not sure whether that is reliable.
Anyone uses that for calculating elapsed time?
What do you mean by "reliable"? The tick count is updated as part of
scheduling during timer interrupts. As long as no one disables interrupts
for more than about 15ms, it is reliable.

However, it's only a 32-bit value, so the number rolls over every 49 days.
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Mar 23 '08 #7
On Mar 22, 10:03 pm, Tim Roberts <t...@probo.comwrote:
Godzilla <godzillais...@gmail.comwrote:
Just found out that win32api.GetTickCount() returns a tick count in
milli-second since XP started. Not sure whether that is reliable.
Anyone uses that for calculating elapsed time?

What do you mean by "reliable"? The tick count is updated as part of
scheduling during timer interrupts. As long as no one disables interrupts
for more than about 15ms, it is reliable.

However, it's only a 32-bit value, so the number rolls over every 49 days.
--
Tim Roberts, t...@probo.com
Providenza & Boekelheide, Inc.
Try getting XP or Vista to stay up for 49 days. I have had Vista stay
up for a little over 11 days. If it doesn't crash, it will have to be
rebooted for some update or other.
Mar 23 '08 #8

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

Similar topics

10
by: John Machin | last post by:
Please consider the timings below, where a generator expression starts out slower than the equivalent list comprehension, and gets worse: >python -m timeit -s "orig=range(100000)"...
17
by: clintonG | last post by:
When the following code is run on Sat Dec 25 2004 19:54:18 GMT-0600 (Central Standard Time) var today = new Date(); var GMTDate = today.toGMTString(); document.write("GMTDate: " + GMTDate); ...
8
by: peterbe | last post by:
What's the difference between time.clock() and time.time() (and please don't say clock() is the CPU clock and time() is the actual time because that doesn't help me at all :) I'm trying to...
6
by: cournape | last post by:
Hi there, I have some scientific application written in python. There is a good deal of list processing, but also some "simple" computation such as basic linear algebra involved. I would like to...
17
by: OlafMeding | last post by:
Below are 2 files that isolate the problem. Note, both programs hang (stop responding) with hyper-threading turned on (a BIOS setting), but work as expected with hyper-threading turned off. ...
5
by: yinglcs | last post by:
Hi, I am following this python example trying to time how long does an operation takes, like this: My question is why the content of the file (dataFile) is just '0.0'? I have tried "print...
1
by: mai | last post by:
Hi everyone, i'm trying to exhibit FIFO anomaly(page replacement algorithm),, I searched over 2000 random strings but i couldnt find any anomaly,, am i I doing it right?,, Please help,,,The...
9
by: Ron Adam | last post by:
I'm having some cross platform issues with timing loops. It seems time.time is better for some computers/platforms and time.clock others, but it's not always clear which, so I came up with the...
8
by: Theo v. Werkhoven | last post by:
hi, In this code I read out an instrument during a user determined period, and save the relative time of the sample (since the start of the test) and the readback value in a csv file. #v+...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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
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
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...

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.