switch.py 842 B

1234567891011121314151617181920212223242526272829
  1. #!/usr/bin/env python
  2. """Switch to a greenlet, which switches back to it's parent over and over again,
  3. and report the elapsed CPU time."""
  4. import optparse
  5. import time
  6. import greenlet
  7. def run_test(n):
  8. def f(*args, **kwargs):
  9. greenlet.getcurrent().parent.switch(*args, **kwargs)
  10. g = greenlet.greenlet(f)
  11. for i in xrange(n):
  12. g.switch(1, 2, 3, a=5, b=6)
  13. if __name__ == '__main__':
  14. p = optparse.OptionParser(
  15. usage='%prog [-n NUM_SWITCHES]', description=__doc__)
  16. p.add_option('-n', type='int', dest='num_switches', default=10000000)
  17. options, args = p.parse_args()
  18. if len(args) != 0:
  19. p.error('unexpected arguments: %s' % ', '.join(args))
  20. start_time = time.clock()
  21. run_test(options.num_switches)
  22. print options.num_switches, 'switches:', time.clock() - start_time, 'sec'