Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
from random import shuffle,choice
from itertools import repeat,chain
from functools import partial
from multiprocessing.pool import Pool
from argparse import ArgumentParser
def cheat(l,x):return True
def findIt1(l,x):
for i in range(50):
if l[i]==x:return True
return False
def findIt2(l,x):
aux=l[x]
for _ in range(50):
if aux==x:return True
aux=l[aux]
return False
def makeTest(f,l):
shuffle(l)
ans=0
for i in range(100):
if f(l,i):ans+=1
return ans//100
makeTest1=partial(makeTest,findIt1)
makeTest2=partial(makeTest,findIt2)
parser=ArgumentParser()
parser.add_argument("n", nargs="?",default=100, type=int ,help="number of iterations, default 100")
parser.add_argument("threads",nargs="?",default=32 ,type=int , help="number of threads in executions, default 32")
args=parser.parse_args()
n=args.n
threads=args.threads
lists=[list(range(100)) for _ in range(threads)]
pool=Pool(threads)
def worker(f):
return sum(pool.map(f,chain.from_iterable(repeat(lists,n))))/(threads*n)
print("cheat test : %g%%"%(worker(partial(makeTest,cheat))*100))
print("avg naive test sequential : %g%%"%(worker(makeTest1)*100))
print("avg test vsuace : %g%%"%(worker(makeTest2)*100))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment