Laughing at myself

Daniel Fackrell unlearned at DELETETHIS.learn2think.org
Tue May 13 12:22:39 EDT 2003


"Alex Martelli" <aleax at aleax.it> wrote in message
news:PZ1wa.86156$3M4.2015056 at news1.tin.it...
> import time
>
> liston = [None] * 1000 * 1000
>
> start = time.time()
> for x in liston: pass
> stend = time.time()
>
> 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
> [alex at lancelot psyco-1.0]$ python ap.py
> 0.237923979759 2.11019694805
> [alex at lancelot psyco-1.0]$ python ap.py
> 0.232311964035 2.13127195835
> [alex at lancelot psyco-1.0]$ python ap.py
> 0.233944058418 2.07932901382
> [alex at lancelot psyco-1.0]$
>
> 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...


Okay, I think I'm slightly puzzled again.  time.sleep(x) appears to behaving
differently depending on the magnitude of x.

For demonstration, here's a modified version of your script with the results
I received:

-----
import time

for sleeptime in (1.0, 0.1, 0.01, 0.001, 0.0001, 0.00001, 0.000001):

    iterations = int(1.0/sleeptime)

    start = time.time()
    for x in xrange(iterations): time.sleep(sleeptime)
    stend = time.time()2

    total = stend-start

    print 'sleeptime: %s, iterations: %s, total: %s' % (sleeptime,
iterations, total)
-----
Win2K, Python 2.2 (IDLE)
sleeptime: 1.0, iterations: 1, total: 1.00100004673
sleeptime: 0.1, iterations: 10, total: 1.00099992752
sleeptime: 0.01, iterations: 100, total: 1.00199997425
sleeptime: 0.001, iterations: 1000, total: 10.0240000486
sleeptime: 0.0001, iterations: 10000, total: 0.039999961853
sleeptime: 1e-005, iterations: 99999, total: 0.380000114441
sleeptime: 1e-006, iterations: 1000000, total: 3.87599992752

Redhat 7.0 (Linux 2.2.16-22):
sleeptime: 1.0, iterations: 1, total: 0.993653059006
sleeptime: 0.1, iterations: 10, total: 0.997701048851
sleeptime: 0.01, iterations: 100, total: 0.998471021652
sleeptime: 0.001, iterations: 1000, total: 9.99859297276
sleeptime: 0.0001, iterations: 10000, total: 99.9983990192
sleeptime: 1e-05, iterations: 99999, total: 1000.03835404
sleeptime: 1e-06, iterations: 1000000, total: 92.6922489405

HPUX:
sleeptime: 1.0, iterations: 1, total: 1.00775301456
sleeptime: 0.1, iterations: 10, total: 1.09865891933
sleeptime: 0.01, iterations: 100, total: 1.99911403656
sleeptime: 0.001, iterations: 1000, total: 9.99897706509
sleeptime: 0.0001, iterations: 10000, total: 100.068940997
sleeptime: 1e-05, iterations: 99999, total: 1000.52884996
(run with sleeptime 1e-06, iterations 1000000 not yet finished, expect
~10000)

I expected that each of the totals would be roughly the same, rather than
off by orders of magnitude from each other.  Did I make some mistake in the
script that is causing this?

--
Daniel Fackrell (newsgroups.NOSPAM at dfackrell.mailshell.com)
When we attempt the impossible, we can experience true growth.






More information about the Python-list mailing list