-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash.py
54 lines (42 loc) · 864 Bytes
/
hash.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from timeit import timeit
from multiprocessing import Pool
from concurrent import futures
import hashlib
import asyncio
data = None
with open('../../data/xyj.txt', 'rt') as fp:
text = fp.read()
data = text.encode()
N = 1000
def hash(_):
m = hashlib.sha256()
m.update(data)
unuse = m.digest()
@timeit
def synchronize():
for _ in range(N):
hash(_)
return N
@timeit
def process():
with Pool(4) as p:
p.map(hash, range(N))
return N
@timeit
def thread():
with futures.ThreadPoolExecutor(4) as p:
p.map(hash, range(N))
return N
@timeit
def asyncs():
async def async_hash():
hash(None)
async def main():
tasks = [async_hash() for i in range(N)]
asyncio.gather(*tasks)
asyncio.run(main(), debug=True)
return N
synchronize()
process()
thread()
asyncs()