我正在尝试使用 python 的dis
库来试验和理解性能。下面是我尝试的一个实验,结果如下。
import dis
def myfunc1(dictionary):
t = tuple(dictionary.items())
return t
def myfunc2(dictionary, func=tuple):
t = func(dictionary.items())
return t
>>> dis.dis(myfunc1)
4 0 LOAD_GLOBAL 0 (tuple)
3 LOAD_FAST 0 (dictionary)
6 LOAD_ATTR 1 (items)
9 CALL_FUNCTION 0
12 CALL_FUNCTION 1
15 STORE_FAST 1 (t)
5 18 LOAD_FAST 1 (t)
21 RETURN_VALUE
>>> dis.dis(myfunc2)
4 0 LOAD_FAST 1 (func)
3 LOAD_FAST 0 (dictionary)
6 LOAD_ATTR 0 (items)
9 CALL_FUNCTION 0
12 CALL_FUNCTION 1
15 STORE_FAST 2 (t)
5 18 LOAD_FAST 2 (t)
21 RETURN_VALUE
现在,我明白了……
- 最左边的 & 是行
4
号5
- 中间的列是机器调用的操作码
- 右边的列是对象(带有opargs?)
...但这一切在性能方面意味着什么?如果我试图决定使用哪个功能,我将如何dis
比较两者?
提前致谢。