在迭代以下生成器表达式时,
fun(i) for i in mylist if i not in setA.union(setB)
是setA.union
在每次迭代中调用的方法,还是只调用一次?
在迭代以下生成器表达式时,
fun(i) for i in mylist if i not in setA.union(setB)
是setA.union
在每次迭代中调用的方法,还是只调用一次?
验证它的另一种方法是对其进行概要分析。借用@AshwiniChaudhary 的示例,我将通过 cProfile 运行它
>>> def func():
a=set([1,2,3])
b=set([3,4,5])
c=[i for i in xrange(10) if i in a.union(b)]
>>> import cProfile
>>> cProfile.run("func()")
13 function calls in 0.000 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.000 0.000 <pyshell#1045>:1(func)
1 0.000 0.000 0.000 0.000 <string>:1(<module>)
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
10 0.000 0.000 0.000 0.000 {method 'union' of 'set' objects}
这里清楚地提到,工会被称为10
时代
是的,setA.union(setB)
在迭代中每次都被调用,
您应该在生成器表达式之前执行此操作并将其存储在变量中
setvalues = setA.union(setB)
fun(i) for i in mylist if i not in setvalues
dis
使用模块的简单证明:
In [24]: def func():
....: a=set([1,2,3])
....: b=set([3,4,5])
....: c=[i for i in xrange(10) if i in a.union(b)]
....:
In [25]: dis.dis(func)
4 42 BUILD_LIST 0
45 LOAD_GLOBAL 1 (xrange)
48 LOAD_CONST 6 (10)
51 CALL_FUNCTION 1
54 GET_ITER #iterator returned from xrange
>> 55 FOR_ITER 33 (to 91) #until the iterator is not exhausted
58 STORE_FAST 2 (i)
61 LOAD_FAST 2 (i)
64 LOAD_FAST 0 (a)
67 LOAD_ATTR 2 (union)
70 LOAD_FAST 1 (b)
73 CALL_FUNCTION 1 #union() is being called in each iteration
76 COMPARE_OP 6 (in)
79 POP_JUMP_IF_FALSE 55
82 LOAD_FAST 2 (i)
85 LIST_APPEND 2
88 JUMP_ABSOLUTE 55
#end of loop
>> 91 STORE_FAST 3 (c)
94 LOAD_CONST 0 (None)
97 RETURN_VALUE
因此,对于您的示例,它将在每次迭代中被调用,即len(mylist)
时间。