Skip to content

Instantly share code, notes, and snippets.

@jrdmcgr
Created February 14, 2013 19:13
Show Gist options
  • Save jrdmcgr/4955436 to your computer and use it in GitHub Desktop.
Save jrdmcgr/4955436 to your computer and use it in GitHub Desktop.
Time some trivial work using various concurrency mechanisms.
""" Time some trivial work using various concurrency mechanisms. """
import time
from threading import Thread
from multiprocessing import Process
def timeit(f):
""" Time a function with this decorator. """
def timed(*args, **kw):
start_time = time.time()
result = f(*args, **kw)
end_time = time.time()
total_time = end_time - start_time
fname = f.__name__.replace('_', ' ').upper()
print '%s: %3.2f sec' % (fname, total_time)
return result
return timed
def work(i=None):
""" Simulate some work. """
# Simulate some pure CPU work.
while i > 2000000:
i -= 1
# Simulate some blocking I/O.
time.sleep(1)
@timeit
def no_concurrency():
for i in range(10):
work(COUNT)
@timeit
def using_threads():
threads = [Thread(target=work) for i in range(10)]
for thread in threads:
thread.start()
for thread in threads:
thread.join()
@timeit
def using_multiprocessing():
processes = [Process(target=work) for i in range(10)]
for process in processes:
process.start()
for process in processes:
process.join()
@timeit
def using_gevent():
# This will turn threads into greenlets
import gevent.monkey; gevent.monkey.patch_all()
threads = [Thread(target=work) for i in range(10)]
for thread in threads:
thread.start()
for thread in threads:
thread.join()
def main():
no_concurrency()
using_threads()
using_multiprocessing()
using_gevent()
if __name__ == '__main__':
main()
@jrdmcgr
Copy link
Author

jrdmcgr commented Feb 14, 2013

NO CONCURRENCY: 11.26 sec
USING THREADS: 3.22 sec
USING MULTIPROCESSING: 1.47 sec
USING GEVENT: 2.33 sec

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment