Attention is currently required from: pespin.
1 comment:
Commit Message:
Patch Set #1, Line 9: - it is a bit faster
I wonder why is it a bit faster? just because the calls are made directly by the compiler in the com […]
I thought it should be a bit faster because of fewer attributes lookup, but unfortunately the version with `with` is slower:
```ipython
In [1]: from threading import Lock
In [2]: mu = Lock()
In [3]: def f():
...: mu.acquire()
...: pass
...: mu.release()
...:
In [4]: def g():
...: with mu:
...: pass
...:
In [5]: from dis import dis
In [6]: dis(f)
1 0 RESUME 0
2 2 LOAD_GLOBAL 0 (mu)
14 LOAD_METHOD 1 (acquire)
36 PRECALL 0
40 CALL 0
50 POP_TOP
3 52 NOP
4 54 LOAD_GLOBAL 0 (mu)
66 LOAD_METHOD 2 (release)
88 PRECALL 0
92 CALL 0
102 POP_TOP
104 LOAD_CONST 0 (None)
106 RETURN_VALUE
In [7]: dis(g)
1 0 RESUME 0
2 2 LOAD_GLOBAL 0 (mu)
14 BEFORE_WITH
16 POP_TOP
3 18 NOP
2 20 LOAD_CONST 0 (None)
22 LOAD_CONST 0 (None)
24 LOAD_CONST 0 (None)
26 PRECALL 2
30 CALL 2
40 POP_TOP
42 LOAD_CONST 0 (None)
44 RETURN_VALUE
>> 46 PUSH_EXC_INFO
48 WITH_EXCEPT_START
50 POP_JUMP_FORWARD_IF_TRUE 4 (to 60)
52 RERAISE 2
>> 54 COPY 3
56 POP_EXCEPT
58 RERAISE 1
>> 60 POP_TOP
62 POP_EXCEPT
64 POP_TOP
66 POP_TOP
68 LOAD_CONST 0 (None)
70 RETURN_VALUE
ExceptionTable:
16 to 16 -> 46 [1] lasti
46 to 52 -> 54 [3] lasti
60 to 60 -> 54 [3] lasti
In [8]: def fff():
...: for i in range(1000):
...: mu.acquire()
...: pass
...: mu.release()
...:
In [9]: def ggg():
...: for i in range(1000):
...: with mu:
...: pass
...:
In [10]: %timeit fff()
84.6 µs ± 3.46 µs per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
In [11]: %timeit ggg()
160 µs ± 1.61 µs per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
In [12]: %timeit fff()
83.6 µs ± 1.01 µs per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
In [13]: %timeit ggg()
160 µs ± 4.68 µs per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
```
I appologize for misinformation.
To view, visit change 39535. To unsubscribe, or for help writing mail filters, visit settings.