Created
May 18, 2019 23:37
-
-
Save giuliano-macedo/68625d265114843253fc01e4b6df9696 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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