Laughing at myself

Terry Reedy tjreedy at udel.edu
Tue May 13 12:53:53 EDT 2003


"Alex Martelli" <aleax at aleax.it> wrote in message
news:PZ1wa.86156$3M4.2015056 at news1.tin.it...
> However, take care about the "apparently perfectly" bit.  E.g.:
>
>
> import time
>
> liston = [None] * 1000 * 1000
>
> start = time.time()
> for x in liston: pass
> stend = time.time()

Wouldn't time.sleep(0) also be an appropriate comparison?

> without_sleep = stend-start
>
> start = time.time()
> for x in liston: time.sleep(0.000001)
> stend = time.time()
>
> with_sleep = stend-start
>
> print without_sleep, with_sleep
>
>
> On this Linux box I observe:
>
> [alex at lancelot psyco-1.0]$ python ap.py
> 0.25779902935 2.6211950779
...
> i.e., the difference between the loops with and without a million
> microsecond sleeps is NOT about one second's elapsed time -- it
> varies by run, but it tends to be closer to TWO seconds here.
> YMMV...

Yeah, on my Windows98 box, sleep(0)  takes 1/18 =.55 seconds -- the
same resolution as time.time()  (time.clock is much better).  If not
for ^D, the difference with 1000*1000 would have been m u u u u u ch
longer.

Terry J. Reedy








More information about the Python-list mailing list