Created
February 14, 2013 19:13
-
-
Save jrdmcgr/4955436 to your computer and use it in GitHub Desktop.
Time some trivial work using various concurrency mechanisms.
This file contains 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
""" 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() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
NO CONCURRENCY: 11.26 sec
USING THREADS: 3.22 sec
USING MULTIPROCESSING: 1.47 sec
USING GEVENT: 2.33 sec