Skip to content

Instantly share code, notes, and snippets.

@thoas
Forked from oleiade/multi_bench.py
Created September 21, 2012 08:32
Show Gist options
  • Save thoas/3760400 to your computer and use it in GitHub Desktop.
Save thoas/3760400 to your computer and use it in GitHub Desktop.
Benchmark single-process, threads, processes in python procedure
import time
import itertools
from fastset.bitvector import bitvector
from Queue import Queue
from threading import Thread
from multiprocessing import Process
class Worker(Thread):
"""Thread executing tasks from a given tasks queue"""
def __init__(self, tasks):
Thread.__init__(self)
self.tasks = tasks
self.daemon = True
self.start()
def run(self):
while True:
func, args, kargs = self.tasks.get()
try:
func(*args, **kargs)
except Exception, e:
print e
self.tasks.task_done()
class ThreadPool:
"""Pool of threads consuming tasks from a queue"""
def __init__(self, num_threads):
self.tasks = Queue(num_threads)
for _ in range(num_threads):
Worker(self.tasks)
def add_task(self, func, *args, **kargs):
"""Add a task to the queue"""
self.tasks.put((func, args, kargs))
def wait_completion(self):
"""Wait for completion of all the tasks in the queue"""
self.tasks.join()
def time_it(function):
"""
Decorator whichs times a function execution.
"""
def wrap(*arg):
start = time.time()
r = function(*arg)
end = time.time()
print "%s (%0.3f ms)" % (function.func_name, (end - start) * 1000)
return r
return wrap
def union(index, bitvectors, res):
res.update({index: reduce(lambda x, y: x & y, bitvectors)})
@time_it
def single_computing(res, bitvectors):
for counter, b in enumerate(bitvectors):
union(counter, b, res)
@time_it
def threads(res, bitvectors, pool):
for counter, b in enumerate(bitvectors):
pool.add_task(union, str(counter), b, res)
pool.wait_completion()
@time_it
def parralel_processing(res, bitvectors):
for counter, b in enumerate(bitvectors):
p = Process(target=union, args=(str(counter), b, res))
p.start()
p.join()
if __name__ == '__main__':
pool = ThreadPool(10)
res = {}
global_bitvectors = []
i, j = 0, 0
print "Generating bitvectors"
while i < 10:
process_bitvectors = [bitvector()] * 30
for b1, b2 in zip(process_bitvectors[::2],
process_bitvectors[1::2]):
while j < 200000:
if j % 2 == 0:
b1[j] = 1
else:
b2[j] = 1
j += 1
global_bitvectors.append(process_bitvectors)
j = 0
i += 1
print "--- SINGLE ---"
print "Computing"
for _ in itertools.repeat(None, 5):
single_computing(res, global_bitvectors)
print "--- DONE ---\n"
print "--- THREADS ---"
print "Computing"
for _ in itertools.repeat(None, 5):
threads(res, global_bitvectors, pool)
print "--- DONE ---"
print "--- MULTI-PROCESS ---"
print "Computing"
for _ in itertools.repeat(None, 5):
parralel_processing(res, global_bitvectors)
print "--- DONE ---"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment