Skip to content

Instantly share code, notes, and snippets.

@arivero
Last active July 1, 2019 17:53
Show Gist options
  • Save arivero/9dd45251a9dcf9b8221f93198d78c265 to your computer and use it in GitHub Desktop.
Save arivero/9dd45251a9dcf9b8221f93198d78c265 to your computer and use it in GitHub Desktop.
Comparacion de las pools de Hilos y de Procesos
import hashlib
import timeit
def slow(z,f,i):
s=hashlib.sha3_512(i.to_bytes(8,'big'))
for x in range(z): s.update(s.digest()*f)
return s.digest()
initcode="""
import concurrent.futures
from functools import partial
executorT = concurrent.futures.ThreadPoolExecutor()
executorP = concurrent.futures.ProcessPoolExecutor()
r=[ x for x in range(240)]
"""
for n in (10,20,30,31,32,33,40,50,60,70,80,90,100,200,400,800,1000,2000,4000,8000):
a=timeit.timeit("len(list(map(partial(slow,10000,n),r)))",setup=initcode, globals=globals(),number=1)
b=timeit.timeit("len(list(executorP.map(partial(slow,10000,n),r)))",setup=initcode, globals=globals(),number=1)
c=timeit.timeit("len(list(executorT.map(partial(slow,10000,n),r)))",setup=initcode, globals=globals(),number=1)
print(n,a,b,c)
python3 abroHilo.py
10 22.996372800320387 2.0461604818701744 46.55920533463359
20 39.46066313609481 3.5965557619929314 81.95272815600038
30 55.201649866998196 5.103452045470476 113.15948644280434
31 56.75447175651789 5.30239475145936 119.12867860496044
32 59.464926317334175 5.5138444527983665 54.21582227572799
33 62.02007649093866 5.611546639353037 54.479837134480476
40 73.6304420940578 6.626953940838575 54.777928579598665
50 87.80760504677892 8.149257827550173 56.79314724355936
60 104.94919126108289 9.852611228823662 56.65379009768367
70 119.42694287002087 11.157399643212557 59.04426611214876
80 135.6387277469039 12.588798444718122 60.72247850522399
90 151.00874296203256 14.422142587602139 63.87562332674861
100 165.55662225931883 15.52572600543499 66.88386274501681
400 640.3550000973046 60.022031493484974 74.77739990502596
800 1272.851788021624 119.09719315171242 144.1262023895979
hashlib libera el GIL a partir de cierto tamaño de buffer, que en nuestros parametros
se corresponde a f=32. No obstante, la liberacion no garantiza que use el 100% de los
cores disponibles inmediatamente.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment