• Sign Up
• Login
• API
• FAQ
• Tools
• Archive
SHARE
TWEET

Untitled

a guest Jun 18th, 2019 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. In [35]: def a(i):
2.     ...:     return i * 2
3.     ...:
4.
5. In [36]: def b():
6.     ...:     return a(i)
7.     ...:
8.
9. In [37]: def c():
10.     ...:     def a(i):
11.     ...:         return i * 2
12.     ...:     return a(2)
13.     ...:
14.
15. In [38]: def d():
16.     ...:     a = lambda i: i * 2
17.     ...:     return a(2)
18.     ...:
19.
20. In [39]: def e():
21.     ...:     i = 2
22.     ...:     return i * 2
23.     ...:
24.
25. In [47]: %timeit b()
26. 177 ns ± 1.06 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
27.
28. In [48]: %timeit c()
29. 261 ns ± 2.54 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
30.
31. In [49]: %timeit d()
32. 260 ns ± 1.05 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
33.
34. In [50]: %timeit e()
35. 120 ns ± 0.13 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
36.
37. In [59]: class C:
38.     ...:     def a(self, i):
39.     ...:         return i * 2
40.     ...:     def b(self):
41.     ...:         return a(2)
42.     ...:
43.
44. In [60]: class D:
45.     ...:     def b(self):
46.     ...:         def a(i):
47.     ...:             return i * 2
48.     ...:         return a(2)
49.     ...:
50.
51. In [61]: obj_c = C()
52.
53. In [62]: obj_d = D()
54.
55. In [63]: %timeit obj_c.b()
56. 202 ns ± 0.628 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
57.
58. In [64]: %timeit obj_d.b()
59. 286 ns ± 1.12 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
60.
61. In [65]: dis.dis("""def a(i):
62.     ...:     return i * 2
63.     ...: def b():
64.     ...:     return a(2)
65.     ...: """)
66.   1           0 LOAD_CONST               0 (<code object a at 0x106ad45d0, file "<dis>", line 1>)
67.               2 LOAD_CONST               1 ('a')
68.               4 MAKE_FUNCTION            0
69.               6 STORE_NAME               0 (a)
70.
71.   3           8 LOAD_CONST               2 (<code object b at 0x1071e44b0, file "<dis>", line 3>)
72.              10 LOAD_CONST               3 ('b')
73.              12 MAKE_FUNCTION            0
74.              14 STORE_NAME               1 (b)
75.              16 LOAD_CONST               4 (None)
76.              18 RETURN_VALUE
77.
78. Disassembly of <code object a at 0x106ad45d0, file "<dis>", line 1>:
79.   2           0 LOAD_FAST                0 (i)
80.               2 LOAD_CONST               1 (2)
81.               4 BINARY_MULTIPLY
82.               6 RETURN_VALUE
83.
84. Disassembly of <code object b at 0x1071e44b0, file "<dis>", line 3>:
85.   4           0 LOAD_GLOBAL              0 (a)
86.               2 LOAD_CONST               1 (2)
87.               4 CALL_FUNCTION            1
88.               6 RETURN_VALUE
89.
90. In [66]: dis.dis("""def b():
91.     ...:     def a(i):
92.     ...:         return i * 2
93.     ...:     return a(2)
94.     ...: """)
95.   1           0 LOAD_CONST               0 (<code object b at 0x1071e4420, file "<dis>", line 1>)
96.               2 LOAD_CONST               1 ('b')
97.               4 MAKE_FUNCTION            0
98.               6 STORE_NAME               0 (b)
99.               8 LOAD_CONST               2 (None)
100.              10 RETURN_VALUE
101.
102. Disassembly of <code object b at 0x1071e4420, file "<dis>", line 1>:
103.   2           0 LOAD_CONST               1 (<code object a at 0x106ad45d0, file "<dis>", line 2>)
104.               2 LOAD_CONST               2 ('b.<locals>.a')
105.               4 MAKE_FUNCTION            0
106.               6 STORE_FAST               0 (a)
107.
108.   4           8 LOAD_FAST                0 (a)
109.              10 LOAD_CONST               3 (2)
110.              12 CALL_FUNCTION            1
111.              14 RETURN_VALUE
112.
113. Disassembly of <code object a at 0x106ad45d0, file "<dis>", line 2>:
114.   3           0 LOAD_FAST                0 (i)
115.               2 LOAD_CONST               1 (2)
116.               4 BINARY_MULTIPLY
117.               6 RETURN_VALUE
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top