Created
April 3, 2012 16:28
-
-
Save methane/2293384 to your computer and use it in GitHub Desktop.
Measure the cost of greenlet switching.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import gevent | |
| import sys | |
| N = int(sys.argv[1]) | |
| M = int(sys.argv[2]) | |
| def sleeper(n): | |
| for i in xrange(N): | |
| #print(n) | |
| gevent.sleep(0) | |
| def sleeper2(n): | |
| hub = gevent.get_hub() | |
| idle = hub.loop.idle(1) | |
| wait = hub.wait | |
| for i in xrange(N): | |
| #print(n) | |
| wait(idle) | |
| greenlets = [gevent.spawn(sleeper2, i) for i in xrange(M)] | |
| gevent.joinall(greenlets) |
Author
Author
sleeper2 inlined gevent.sleep(). This makes significant speedup.
$ time python switchbench.py 1000 1000
real 0m7.891s
user 0m7.844s
sys 0m0.032s
Real Python application has more Python code. So the cost of greenlet switching is small relatively.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In my environment,