473,721 Members | 2,254 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

High resolution sleep (Linux)


The table below shows the execution time for this code snippet as
measured by the unix command `time':

for i in range(1000):
time.sleep(inte r)

inter execution time ideal
0 0.02 s 0 s
1e-4 4.29 s 0.1 s
1e-3 4.02 s 1 s
2e-3 4.02 s 2 s
5e-3 8.02 s 5 s

Hence it seems like the 4 s is just overhead and that the time.sleep
method treats values below approximately 0.001 as 0.

Is there a standard way (or slick trick) to get higher resolution? If
it is system dependent it's acceptable but not very nice :)

May 4 '07 #1
19 14996
John <ja****@gmail.c omwrote:
>
The table below shows the execution time for this code snippet as
measured by the unix command `time':

for i in range(1000):
time.sleep(inte r)

inter execution time ideal
0 0.02 s 0 s
1e-4 4.29 s 0.1 s
1e-3 4.02 s 1 s
2e-3 4.02 s 2 s
5e-3 8.02 s 5 s

Hence it seems like the 4 s is just overhead and that the time.sleep
method treats values below approximately 0.001 as 0.
The exact minimum sleep time will be determined by the HZ value that
your kernel was compiled with. In newer kernels that is typically 250
or 1000, it used to be 100 in older kernels.

If you look at the config file used to build your kernel you can
discover the HZ value.

If you try to sleep for less than a timer tick, then the kernel
performs a yield.
Is there a standard way (or slick trick) to get higher resolution? If
it is system dependent it's acceptable but not very nice :)
I assume you are using sleep for timing purposes.. This isn't a
terribly good idea, but here is an idea for you....

# ------------------------------------------------------------
from time import time, sleep

def hr_sleep(durati on):
end = time() + duration
if duration 0.02:
sleep(duration)
while time() - end < 0:
sleep(0)

if __name__ == "__main__":
from timeit import Timer
for e in range(0,7):
dt = 10**-e
loops = 10**e
t = Timer("hr_sleep (%f)" % dt, "from __main__ import hr_sleep")
print "expected = %.2E actual = %.2E" % (dt, t.timeit(loops)/loops)
# ------------------------------------------------------------

This prints

expected = 1.00E+00 actual = 1.00E+00
expected = 1.00E-01 actual = 1.00E-01
expected = 1.00E-02 actual = 1.00E-02
expected = 1.00E-03 actual = 1.00E-03
expected = 1.00E-04 actual = 1.02E-04
expected = 1.00E-05 actual = 1.19E-05
expected = 1.00E-06 actual = 2.66E-06

on my 250 HZ machine

You could then do run-time calibration to work out the overhead of the
function on any given machine to make it more accurate.

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
May 5 '07 #2
John <ja****@gmail.c omwrote:
>
The table below shows the execution time for this code snippet as
measured by the unix command `time':

for i in range(1000):
time.sleep(inte r)

inter execution time ideal
0 0.02 s 0 s
1e-4 4.29 s 0.1 s
1e-3 4.02 s 1 s
2e-3 4.02 s 2 s
5e-3 8.02 s 5 s

Hence it seems like the 4 s is just overhead and that the time.sleep
method treats values below approximately 0.001 as 0.

Is there a standard way (or slick trick) to get higher resolution? If
it is system dependent it's acceptable but not very nice :)
Consider what you're asking here. The operating system can only age the
timer list and re-evaluate process ready states when a process goes into
kernel mode, either by releasing the CPU or hitting the end of its time
slice. In order to know that a process has reached the end of its time
slice, it has to be interrupted by something, typically the timer
interrupt.

In order to provide 10us sleep resolution, the timer interrupt would have
to fire every 10us. The overhead of handling the timer interrupt and
rescheduling that often is quite significant.
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
May 5 '07 #3
"Tim Roberts" <ti..@pr...o.co mwrote"
Consider what you're asking here. The operating system can only age the
timer list and re-evaluate process ready states when a process goes into
kernel mode, either by releasing the CPU or hitting the end of its time
slice. In order to know that a process has reached the end of its time
slice, it has to be interrupted by something, typically the timer
interrupt.
Yes
>
In order to provide 10us sleep resolution, the timer interrupt would have
to fire every 10us.
Not necessarily. see below
The overhead of handling the timer interrupt and
rescheduling that often is quite significant.
Yes

It is also possible to keep the timer list sorted by "expiry date",
and to reprogram the timer to interrupt at the next expiry time
to give arbitrary resolution, instead of implementing a regular 'tick'.
But this also adds overhead and its a PITA to do efficiently,
using a linked list of "next interrupt time". So its not normally
done unless you *really* need it. Its easier to make a free
running hardware counter and to read it and do the sums
yourself, hogging the processor, if you need such fine resolution.

Ten microseconds is not much time - Speed of light is about
one foot per nanosecond, so that gives ten thousand feet of
travel for a radio wave - less than two miles, or about three
kilometres.

A rifle bullet can travel at around 5000 feet per second.
In ten microseconds it moves six tenths of an inch.
A vehicle at 300 Km/h (about 187 MPH) will not move
as much as a millimetre in that time.

OTOH - if you are trying to make a software radar system to
pick up intruders in your back yard, then ten microseconds
is a hopelessly long time...

- Hendrik
May 7 '07 #4
"Dennis Lee Bieber" <w..d@ix.n,..m. comwrote:
On Sun, 6 May 2007 10:15:26 +0200, "Hendrik van Rooyen"
<m..@mi..p.co.z adeclaimed the following in comp.lang.pytho n:

A rifle bullet can travel at around 5000 feet per second.

You've got some fast rifles over there...
LOL - true - I stand corrected - I was aware that:

1) All the animals were slaughtered, and the American Civil
War fought with rifles of muzzle velocity around 1800 fps.
This was before bullets were jacketed - if you try to push a
lead slug through a rifled barrel faster than this, it strips and
fouls the barrel

2) That the old NATO round (.308 Winchester) travels at
around 2500 fps. - and this was some forty years ago,
when I did my stint of military duty.

So being an idle bugger, I just naturally assumed that the
speed would have doubled in the intervening time since
I was last involved in this subject. - hence the 5000.

Did you know that the first military smokeless powder
round was for the French Lebel? - It threw a bronze
ball, and could punch through a single brick wall.

Battlefields were suddenly far more dangerous places.

- Hendrik
May 8 '07 #5
On Tue, 08 May 2007 17:59:13 +0000, Dennis Lee Bieber wrote:
>Did you know that the first military smokeless powder
round was for the French Lebel? - It threw a bronze
ball, and could punch through a single brick wall.
Well, extreme high speed wouldn't help for that -- just get a
surface splatter. Heavy and slower... (or some sort of solid core --
depleted uranium with a teflon coating)
I remember a MythBusters episode that had the guys testing the old
Hollywood staple of somebody trying to escape gunfire by swimming
underwater. To their surprise, they found that modern high-velocity rounds
basically hit the water and stopped dead, hardly penetrating at all, while
an old musket shot they found actually penetrated the water furthest.

Hmmm... musket? I may be confabulating that last bit, the rifle may not
have been that old. World War One vintage perhaps? But it fired a heavy
slow round, and everybody predicted it would penetrate the water the
least, but it was the opposite.

Anyway, the myth was confirmed. I guess that's why people don't go fishing
with shotguns.
--
Steven.

May 8 '07 #6
On May 8, 12:59 pm, Dennis Lee Bieber <wlfr...@ix.net com.comwrote:
On Tue, 8 May 2007 08:24:01 +0200, "Hendrik van Rooyen"
<m...@microcorp .co.zadeclaimed the following in comp.lang.pytho n:
So being an idle bugger, I just naturally assumed that the
speed would have doubled in the intervening time since
I was last involved in this subject. - hence the 5000.

Development tends to go in both directions... Massive rounds moving
at, or slightly slower, than "typical" (2000fps) vs very light rounds
moving at really high speeds (the various .17).
Did you know that the first military smokeless powder
round was for the French Lebel? - It threw a bronze
ball, and could punch through a single brick wall.

Well, extreme high speed wouldn't help for that -- just get a
surface splatter. Heavy and slower... (or some sort of solid core --
depleted uranium with a teflon coating)
And penetration isn't always desireable. A slow, heavy round will
punch a tunnel through your arm muscle, but a fast, light round
will take your entire arm off.
--
Wulfraed Dennis Lee Bieber KD6MOG
wlfr...@ix.netc om.com wulfr...@bestia ria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: web-a...@bestiaria. com)
HTTP://www.bestiaria.com/

May 9 '07 #7
Hendrik van Rooyen wrote:
"Tim Roberts" <ti..@pr...o.co mwrote"
It is also possible to keep the timer list sorted by "expiry date",
and to reprogram the timer to interrupt at the next expiry time
to give arbitrary resolution, instead of implementing a regular 'tick'.
Yes, and that's a common feature in real-time operating systems.
If you're running QNX, you can expect that if your high priority
task delays to a given time, you WILL get control back within a
millisecond of the scheduled time. Even tighter timing control
is available on some non-x86 processors.

Some CPUs even have hardware support for a sorted event list.
The Intel 8061, which ran the engines of most Ford cars in the 1980s,
had that.

But no way are you going to get consistent timing resolution like that
from Python. It's an interpreter with a garbage collector, after all.

John Nagle
May 9 '07 #8
On 9 Maj, 03:23, John Nagle <n...@animats.c omwrote:
Hendrik van Rooyen wrote:
"Tim Roberts" <ti..@pr...o.co mwrote"
It is also possible to keep the timer list sorted by "expiry date",
and to reprogram the timer to interrupt at the next expiry time
to give arbitrary resolution, instead of implementing a regular 'tick'.

Yes, and that's a common feature in real-time operating systems.
If you're running QNX, you can expect that if your high priority
task delays to a given time, you WILL get control back within a
millisecond of the scheduled time. Even tighter timing control
is available on some non-x86 processors.

Some CPUs even have hardware support for a sorted event list.
The Intel 8061, which ran the engines of most Ford cars in the 1980s,
had that.

But no way are you going to get consistent timing resolution like that
from Python. It's an interpreter with a garbage collector, after all.

John Nagle

The application the original poster (i.e. me) was interested in was a
program that sends ethernet packets at a loosely specified rate. A
loop that sends all packets with no sleep in between will send them at
a too high rate. Using the default sleep in my Python interpreter
sleeps to long, since even a few microseconds add up when you send
hundreds of thousands of packets.

If the process scheduler deals with another process now and then, it
doesn't matter. If it switches to another application between each
packet is beeing sent, that's a problem.

Anyways, what I need is high resolution sleep, not high resolution
timing. Installing a real time OS seems like overkill.

(Yes I know, one can also send, say, 50 packets at a time, and then
sleep, send 50 more packets, and so on.)

May 10 '07 #9
On 9 Maj, 03:23, John Nagle <n...@animats.c omwrote:
Hendrik van Rooyen wrote:
"Tim Roberts" <ti..@pr...o.co mwrote"
It is also possible to keep the timer list sorted by "expiry date",
and to reprogram the timer to interrupt at the next expiry time
to give arbitrary resolution, instead of implementing a regular 'tick'.
Yes, and that's a common feature in real-time operating systems.
If you're running QNX, you can expect that if your high priority
task delays to a given time, you WILL get control back within a
millisecond of the scheduled time. Even tighter timing control
is available on some non-x86 processors.

Some CPUs even have hardware support for a sorted event list.
The Intel 8061, which ran the engines of most Ford cars in the 1980s,
had that.

But no way are you going to get consistent timing resolution like that
from Python. It's an interpreter with a garbage collector, after all.

John Nagle

The application the original poster (i.e. me) was interested in was a
program that sends ethernet packets at a loosely specified rate. A
loop that sends all packets with no sleep in between will send them at
a too high rate. Using the default sleep in my Python interpreter
sleeps to long, since even a few microseconds add up when you send
hundreds of thousands of packets.

If the process scheduler deals with another process now and then, it
doesn't matter. If it switches to another application between each
packet is beeing sent, that's a problem.

Anyways, what I need is high resolution sleep, not high resolution
timing. Installing a real time OS seems like overkill..

(Yes I know, one can also send, say, 50 packets at a time, and then
sleep, send 50 more packets, and so on.)

--
http://mail.python.org/mailman/listinfo/python-list
What about C module with usleep,nanoslee p?
Best regards.
Bart.

May 10 '07 #10

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

Similar topics

1
3521
by: Uwe Mayer | last post by:
Hi, I need a function returning a time value with a higher resolution that the standard 1sec unix timestamp. I found the clock() function in the time module, but that seems to return the same value (in the Python shell) each time I call it (Debian Linux speaking here). Any ideas? Thanks,
2
2182
by: Ben Amada | last post by:
Hi group. I'm going to display a low resolution image in an HTML page. On the web server, I have a high resolution version of that image. If I display the high resolution image in the browser for the visitor to print, won't the DPI of the image be reduced to 72 dpi and lose resolution when the visitor goes to print the image? If this is wrong, please let me know. If this is correct, is there a way in JavaScript to be able to send the...
2
733
by: Michael Evans | last post by:
First, we rely on a stable update rate so that our physics and dynamics calculations and integrations are based on a known interval and therefore are true-to-life. Second, the graphics positions in our image generators are updated by this dynamics application, and we need to update them at the rate they are being refreshed so that we don't get any stutter from frames being used twice or having to be thrown out. Without microsecond...
3
6230
by: Michael Evans | last post by:
Has anyone come across a way to sleep (suspend execution of current process while releasing CPU resources in the meantime) for less than a millisecond in Windows? I know it has been done by third parties such as with the XBO LLADRV project, but I cannot get even enough support from them to enable their software in my project. Ideally, I need something like the unix usleep() function which will do a processor-friendly sleep for some...
6
5115
by: moondaddy | last post by:
I have an application where I need to print images where the size has been reduced down to a fraction of a thumbnail and the original image is several inches high and wide. We're using a special printing process where the dpi is 2000x2000 or higher, therefore, when I reduce the size of the image, I don't want to loose any pixels. In other words, if the image is 200 px by 200 px and I reduce the size down to 1/4 inch, I want the printer to...
17
6426
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. Note, the Windows task manager shows 2 CPUs on the Performance tab with hyper-threading is turned on. Both Python 2.3.5 and 2.4.3 (downloaded from python.org) have this problem. The operating system is MS Windows XP Professional.
4
5404
by: Dmitri Sologoubenko | last post by:
Hi, guys! I need a C++ class (Linux/POSIX/GNU C++), which can be started and stopped, and calls a virtual callback instance method (e.g. "expired()") when a given time has elapsed. High resolution means that expire time should be measured in microseconds, or at least in milliseconds. I had a look on Gnu C <sys/time.h>'s functions getitimer and setitimer, but these all use a signal (SIGALRM) to notify expiration, and I don't know the...
19
3444
by: Jon Slaughter | last post by:
Is it possible to have a synchronous thread... actually a timer that is beyond the 1khz/1ms resolution that .NET offers? I want to poll the parallel port at rates beyond 1khz... about 10 to 100 times that rate if possible. Essnetially I want to monitor the parallel port for data and display it but I need a fast but somewhat precise way of knowing the sample rate. Doing some tests on just a simple thread I can get speeds at around...
13
5834
by: Charles Zhang | last post by:
Sleep() function Sleep at lease 1 millisecond, there is a way to make a thread to sleep less than a millisecond? One way I know of is using performance counter which is not really sleep ( loop and check again and again and uses CPU time). Thanks Charles Zhang
0
8840
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8730
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9215
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9131
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6669
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5981
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3189
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2130
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.