[python-win32] sleep() fine-control in Python - RDTSC, select() etc.

Ray S rays at blue-cove.com
Tue Jan 25 01:32:35 CET 2005


Skipped content of type multipart/alternative-------------- next part --------------
import time

mytime = time.time ## optimization
sleep = time.sleep
clock = time.clock

def mydelay(delay):
    global logical_time
    physical_time = mytime()
    try:
        logical_time = logical_time + delay
    except NameError:
        logical_time = physical_time + delay
    if logical_time > physical_time:
        sleep(logical_time - physical_time)
        #sleep(.0001) ## same as sleep(0)!
        #sleep(.001)


p = 0.
d1 = 0.
d2 = 0.
looped = 0.
howMany = 100
for i in range(20):
    ## timer for pass in a for loop
    t1 = clock()
    for j in range(howMany):
        pass
    p += (clock()-t1)

    t1 = clock()
    for j in range(howMany):
        mydelay(.0000001)     ## ~minumum useable
    d1 += (clock()-t1)

    t1 = clock()
    for j in range(howMany):
        mydelay(.00016)      ## one turbine degree
    d2 += (clock()-t1)

    looped+= howMany
    print 'micro sec: %.1f %.1f %.1f' % (1000000*p/looped, 1000000*d1/looped, 1000000*(d2)/looped)
-------------- next part --------------
import time
import select
import socket

HOST = ''                 # Symbolic name meaning the local host
PORT = 50007              # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

mytime = time.time ## optimization
sleep = time.sleep
clock = time.clock


p = 0.
d1 = 0.
d2 = 0.
looped = 0.
howMany = 100
for i in range(20):
    ## timer for pass in a for loop
    t1 = clock()
    for j in range(howMany):
        pass
    p += (clock()-t1)

    t1 = clock()
    for j in range(howMany):
        select.select([s], [], [], .0000009)
        #select.select([s], [], [], .000001) ## causes a timing jump!
    d1 += (clock()-t1)

    t1 = clock()
    for j in range(howMany):
        select.select([s], [], [], .0004)
    d2 += (clock()-t1)

    looped+= howMany
    print 'micro sec: %.1f %.1f %.1f' % (1000000*p/looped, 1000000*d1/looped, 1000000*(d2)/looped)
-------------- next part --------------
// dosDelay.cpp : Defines the entry point for the console application.


struct sysclock_t {
   int LL, LH, HL, HH;
};

struct tv time;
time.tv_sec = num_seconds_to_sleep;
time.tv_usec = num_microseconds_to_sleep;

select(NULL,NULL,NULL,& time);

void main()
{
   int ns= 1000000;
   // make the sleep in 100th nanoseconds.
   ns = (ns + 99)/100;
   //ns -= FACTOR;

   __asm {
      rdtsc
      // clock in EDX:EAX
      // we ignore EDX since we asume ns is
      // of shorter duration than that.
      add eax,dword ptr ns
      mov ebx,eax // save eax into ebx.
      jnc loop2

      // new time is after wrapping around
loop1:
      rdtsc
      // again we ignore EDX
      // wait for time to wrap around.
      cmp eax,ebx
      jae loop1

// Time has wrapped around and present time < wait time.
loop2:
      rdtsc
      // again we ignore EDX
      cmp eax,ebx
      jb loop2
    }
}


More information about the Python-win32 mailing list