This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Speed up Thread.start()
Type: behavior Stage:
Components: Library (Lib) Versions: Python 2.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: jyasskin Nosy List: jyasskin
Priority: normal Keywords: patch

Created on 2008-02-25 02:30 by jyasskin, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
faster_thread_startup.diff jyasskin, 2008-02-25 02:30
Messages (2)
msg62963 - (view) Author: Jeffrey Yasskin (jyasskin) * (Python committer) Date: 2008-02-25 02:30
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.
msg63089 - (view) Author: Jeffrey Yasskin (jyasskin) * (Python committer) Date: 2008-02-28 06:10
Submitted as r61100.
History
Date User Action Args
2022-04-11 14:56:31adminsetgithub: 46437
2008-02-28 06:10:15jyasskinsetstatus: open -> closed
assignee: jyasskin
resolution: fixed
messages: + msg63089
keywords: patch, patch
2008-02-25 02:30:23jyasskincreate