-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocking2.py
47 lines (39 loc) · 1005 Bytes
/
locking2.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
# Based on snippet from https://bugs.python.org/issue3001
REPEATS = 1000000
def do_nothing():
pass
def RLockSpeed():
import threading
import time
t = time.time()
result = {}
for i in range(REPEATS):
do_nothing()
result["empty loop"] = time.time() - t
lock = threading.Lock()
t = time.time()
for i in range(REPEATS):
lock.acquire()
do_nothing()
lock.release()
result["Lock"] = time.time() - t
t = time.time()
for i in range(REPEATS):
with lock:
do_nothing()
result["Lock_context"] = time.time() - t
lock = threading.RLock()
t = time.time()
for i in range(REPEATS):
lock.acquire()
do_nothing()
lock.release()
result["RLock"] = time.time() - t
t = time.time()
for i in range(REPEATS):
with lock:
do_nothing()
result["RLock_context"] = time.time() - t
return result
if __name__ == "__main__":
print(RLockSpeed())