gpt4 book ai didi

python - 为什么这两个函数在dis.dis下反汇编后字节码是一样的?

转载 作者:太空狗 更新时间:2023-10-29 21:41:21 25 4
gpt4 key购买 nike

接下来是四个具有相同输出的函数,但要么是用列表推导式编写的,要么是用紧密循环编写的,以及对 vs 内联条件的函数调用。

有趣的是,ab 在反汇编时具有相同的字节码,但是ba 快得多.

此外,d 使用没有函数调用的紧密循环,比使用列表理解和函数调用的 a 更快。​​

为什么函数 a 和 b 具有相同的字节码,为什么 b 的性能比给定相同字节码的 a 好得多?

import dis

def my_filter(n):
return n < 5

def a():
# list comprehension with function call
return [i for i in range(10) if my_filter(i)]

def b():
# list comprehension without function call
return [i for i in range(10) if i < 5]

def c():
# tight loop with function call
values = []
for i in range(10):
if my_filter(i):
values.append(i)
return values

def d():
# tight loop without function call
values = []
for i in range(10):
if i < 5:
values.append(i)
return values

assert a() == b() == c() == d()

import sys
>>> sys.version_info[:]
(3, 6, 5, 'final', 0)

# list comprehension with function call
>>> dis.dis(a)
2 0 LOAD_CONST 1 (<code object <listcomp> at 0x00000211CBE8B300, file "<stdin>", line 2>)
2 LOAD_CONST 2 ('a.<locals>.<listcomp>')
4 MAKE_FUNCTION 0
6 LOAD_GLOBAL 0 (range)
8 LOAD_CONST 3 (10)
10 CALL_FUNCTION 1
12 GET_ITER
14 CALL_FUNCTION 1
16 RETURN_VALUE

# list comprehension without function call
>>> dis.dis(b)
2 0 LOAD_CONST 1 (<code object <listcomp> at 0x00000211CBB64270, file "<stdin>", line 2>)
2 LOAD_CONST 2 ('b.<locals>.<listcomp>')
4 MAKE_FUNCTION 0
6 LOAD_GLOBAL 0 (range)
8 LOAD_CONST 3 (10)
10 CALL_FUNCTION 1
12 GET_ITER
14 CALL_FUNCTION 1
16 RETURN_VALUE

# a and b have the same byte code?
# Why doesn't a have a LOAD_GLOBAL (my_filter) and CALL_FUNCTION?
# c below has both of these

# tight loop with function call
>>> dis.dis(c)
2 0 BUILD_LIST 0
2 STORE_FAST 0 (values)

3 4 SETUP_LOOP 34 (to 40)
6 LOAD_GLOBAL 0 (range)
8 LOAD_CONST 1 (10)
10 CALL_FUNCTION 1
12 GET_ITER
>> 14 FOR_ITER 22 (to 38)
16 STORE_FAST 1 (i)

4 18 LOAD_GLOBAL 1 (my_filter)
20 LOAD_FAST 1 (i)
22 CALL_FUNCTION 1
24 POP_JUMP_IF_FALSE 14

5 26 LOAD_FAST 0 (values)
28 LOAD_ATTR 2 (append)
30 LOAD_FAST 1 (i)
32 CALL_FUNCTION 1
34 POP_TOP
36 JUMP_ABSOLUTE 14
>> 38 POP_BLOCK

6 >> 40 LOAD_FAST 0 (values)
42 RETURN_VALUE

# tight loop without function call
>>> dis.dis(d)
2 0 BUILD_LIST 0
2 STORE_FAST 0 (values)

3 4 SETUP_LOOP 34 (to 40)
6 LOAD_GLOBAL 0 (range)
8 LOAD_CONST 1 (10)
10 CALL_FUNCTION 1
12 GET_ITER
>> 14 FOR_ITER 22 (to 38)
16 STORE_FAST 1 (i)

4 18 LOAD_FAST 1 (i)
20 LOAD_CONST 2 (5)
22 COMPARE_OP 0 (<)
24 POP_JUMP_IF_FALSE 14

5 26 LOAD_FAST 0 (values)
28 LOAD_ATTR 1 (append)
30 LOAD_FAST 1 (i)
32 CALL_FUNCTION 1
34 POP_TOP
36 JUMP_ABSOLUTE 14
>> 38 POP_BLOCK

6 >> 40 LOAD_FAST 0 (values)
42 RETURN_VALUE

import timeit

>>> timeit.timeit(a) # list comprehension with my_filter
1.2435139456834463
>>> timeit.timeit(b) # list comprehension without my_filter
0.6717423789164627
>>> timeit.timeit(c) # no list comprehension with my_filter
1.326850592144865
>>> timeit.timeit(d) # no list comprehension no my_filter
0.7743895521070954

为什么ab反汇编后字节码相同?我希望 b 有更好看的字节码。值得注意的是,我会认为 a 需要一个 LOAD_GLOBAL 吗? (my_filter) 和一个 CALL FUNCTION。例如,ca 相同,但没有列表理解,它在地址 18 和 22 上使用这些字节码。

然而,即使使用相同的字节码,b 的性能也比 a 好得多。这是怎么回事?

更有趣的是,d 使用紧密循环但没有调用 my_filter,它比使用 b 更快列表理解,但调用了 my_filter。看起来使用函数的开销超过了紧密循环的开销。

我的目标是尝试弄清楚我是否可以将列表理解的条件分解为一个函数,以使列表理解更易于阅读。

最佳答案

请注意 a 的两个字节码和 b只运行 <listcomp>在别处定义的对象。

2           0 LOAD_CONST               1 (<code object <listcomp> at 0x00000211CBE8B300, file "<stdin>", line 2>)

由于包装函数 ab完全相同,字节码相同,只是listcomps的地址不同。

在 python 3.7 中,dis 模块还打印 listcomps,这是完整的代码和输出:

import sys
import dis

def my_filter(n):
return n < 5

def a():
# list comprehension with function call
return [i for i in range(10) if my_filter(i)]

def b():
# list comprehension without function call
return [i for i in range(10) if i < 5]

print(sys.version)
print('-' * 70)
dis.dis(a)
print('-' * 70)
dis.dis(b)

--

3.7.3 (default, May 19 2019, 21:16:26) 
[Clang 10.0.1 (clang-1001.0.46.4)]
----------------------------------------------------------------------
9 0 LOAD_CONST 1 (<code object <listcomp> at 0x1065c61e0, file "/w/test/x.py", line 9>)
2 LOAD_CONST 2 ('a.<locals>.<listcomp>')
4 MAKE_FUNCTION 0
6 LOAD_GLOBAL 0 (range)
8 LOAD_CONST 3 (10)
10 CALL_FUNCTION 1
12 GET_ITER
14 CALL_FUNCTION 1
16 RETURN_VALUE

Disassembly of <code object <listcomp> at 0x1065c61e0, file "/w/test/x.py", line 9>:
9 0 BUILD_LIST 0
2 LOAD_FAST 0 (.0)
>> 4 FOR_ITER 16 (to 22)
6 STORE_FAST 1 (i)
8 LOAD_GLOBAL 0 (my_filter)
10 LOAD_FAST 1 (i)
12 CALL_FUNCTION 1
14 POP_JUMP_IF_FALSE 4
16 LOAD_FAST 1 (i)
18 LIST_APPEND 2
20 JUMP_ABSOLUTE 4
>> 22 RETURN_VALUE
----------------------------------------------------------------------
13 0 LOAD_CONST 1 (<code object <listcomp> at 0x1066188a0, file "/w/test/x.py", line 13>)
2 LOAD_CONST 2 ('b.<locals>.<listcomp>')
4 MAKE_FUNCTION 0
6 LOAD_GLOBAL 0 (range)
8 LOAD_CONST 3 (10)
10 CALL_FUNCTION 1
12 GET_ITER
14 CALL_FUNCTION 1
16 RETURN_VALUE

Disassembly of <code object <listcomp> at 0x1066188a0, file "/w/test/x.py", line 13>:
13 0 BUILD_LIST 0
2 LOAD_FAST 0 (.0)
>> 4 FOR_ITER 16 (to 22)
6 STORE_FAST 1 (i)
8 LOAD_FAST 1 (i)
10 LOAD_CONST 0 (5)
12 COMPARE_OP 0 (<)
14 POP_JUMP_IF_FALSE 4
16 LOAD_FAST 1 (i)
18 LIST_APPEND 2
20 JUMP_ABSOLUTE 4
>> 22 RETURN_VALUE

对于 pythons < 3.7。见Python: analyze a list comprehension with dis

关于python - 为什么这两个函数在dis.dis下反汇编后字节码是一样的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57647670/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com