Skip to content

Instantly share code, notes, and snippets.

@arbennett
Created May 29, 2016 21:44
Show Gist options
  • Save arbennett/9ae36959038f05cf5a86a9354ab6fd2a to your computer and use it in GitHub Desktop.
Save arbennett/9ae36959038f05cf5a86a9354ab6fd2a to your computer and use it in GitHub Desktop.
Just testing for efficiency across various synchronization schemes for multiprocessing datastructures
"""
Just testing for efficiency across various synchronization schemes
for multiprocessing datastructures
"""
import time
import queue
import multiprocessing as mp
def dict_increments(d, q, s):
""" Incrementally add to a dictionary """
t0 = time.clock()
d["First"] = dict()
for i in range(s):
d["First"][str(i)] = i
t1 = time.clock()
q.put("Incremental Dict: " + str(t1-t0) + " seconds\n")
def dict_batch(d, q, s):
""" Create a new dictionary and then add it as a sub-dict """
t0 = time.clock()
the_dict = dict()
for i in range(s):
the_dict[str(i)] = i
d["First"] = the_dict
t1 = time.clock()
q.put("Batch Dict: " + str(t1-t0) + " seconds\n")
def list_increments(l, q, s):
""" Incrementally add to a list """
t0 = time.clock()
for i in range(s):
l.append(i)
t1 = time.clock()
q.put("Incremental List: " + str(t1-t0) + " seconds\n")
def list_batch(l, q, s):
""" Create a new list and then add it to an existing list """
t0 = time.clock()
nl = []
for i in range(s):
nl.append(i)
l += nl
t1 = time.clock()
q.put("Batch List: " + str(t1-t0) + " seconds\n")
def main():
print("")
SIZE = 10000
manager = mp.Manager()
q = mp.Queue()
inc_dict = manager.dict()
bat_dict = manager.dict()
inc_list = manager.list()
bat_list = manager.list()
print("===================== Testing Multiprocesssing ======================")
process_handles = [
mp.Process(target=dict_increments, args=(inc_dict, q, SIZE)),
mp.Process(target=dict_batch, args=(bat_dict, q, SIZE)),
mp.Process(target=list_increments, args=(inc_list, q, SIZE)),
mp.Process(target=list_batch, args=(bat_list, q, SIZE))
]
for p in process_handles:
p.start()
for p in process_handles:
p.join()
while not q.empty():
print(q.get())
print("========================= Testing Serial ============================")
q = queue.Queue()
dict_increments(dict(), q, SIZE)
dict_batch(dict(), q, SIZE)
list_increments([], q, SIZE)
list_batch([], q, SIZE)
while not q.empty():
print(q.get())
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment