[New-bugs-announce] [issue2184] Speed up Thread.start()

Jeffrey Yasskin report at bugs.python.org
Mon Feb 25 03:30:23 CET 2008


New submission from Jeffrey Yasskin:

Thread.start() used sleep(0.000001) to make sure it didn't return before
the new thread had started. At least on my MacBook Pro, that wound up
sleeping for a full 10ms (probably 1 jiffy). By using an Event instead,
we can be absolutely certain that the thread has started, and return
more quickly (217us).

Before:
$  ./python.exe -m timeit -s 'from threading import Thread'  't =
Thread(); t.start(); t.join()'
100 loops, best of 3: 10.3 msec per loop
$  ./python.exe -m timeit -s 'from threading import Thread; t =
Thread()'  't.isAlive()'
1000000 loops, best of 3: 0.47 usec per loop

After:
$  ./python.exe -m timeit -s 'from threading import Thread'  't =
Thread(); t.start(); t.join()'
1000 loops, best of 3: 217 usec per loop
$  ./python.exe -m timeit -s 'from threading import Thread; t =
Thread()'  't.isAlive()'
1000000 loops, best of 3: 0.86 usec per loop

To be fair, the 10ms isn't CPU time, and other threads including the
spawned one get to run during it. There are also some slightly more
complicated ways to get back the .4us in isAlive() if we want.

----------
components: Library (Lib)
files: faster_thread_startup.diff
keywords: patch, patch
messages: 62963
nosy: jyasskin
severity: normal
status: open
title: Speed up Thread.start()
type: behavior
versions: Python 2.6
Added file: http://bugs.python.org/file9546/faster_thread_startup.diff

__________________________________
Tracker <report at bugs.python.org>
<http://bugs.python.org/issue2184>
__________________________________


More information about the New-bugs-announce mailing list