# python中列表长度可变吗_可以在Python中更快地对可变长度迭代的简单计算吗？

def test1(u,v):

return (u[0]-v[0])**2 + (u[1]-v[1])**2 + (u[3]-v[3])**2

dis.dis(test1)

6 BINARY_SUBSCR

13 BINARY_SUBSCR

14 BINARY_SUBTRACT

18 BINARY_POWER

25 BINARY_SUBSCR

32 BINARY_SUBSCR

33 BINARY_SUBTRACT

37 BINARY_POWER

45 BINARY_SUBSCR

52 BINARY_SUBSCR

53 BINARY_SUBTRACT

57 BINARY_POWER

59 RETURN_VALUE

def test2(u,v):

sum((a-b)**2 for a,v))

dis.dis(test2)

3 LOAD_CONST 1 ( at 02C6F458,file "",line 2>)

6 MAKE_FUNCTION 0

18 CALL_FUNCTION 2

21 GET_ITER

22 CALL_FUNCTION 1

25 CALL_FUNCTION 1

28 RETURN_VALUE

def test3(u,v):

sum = 0

for i in xrange(len(u)):

sum += (u[i]-v[i])**2

dis.dis(test3)

3 STORE_FAST 2 (sum)

6 SETUP_LOOP 52 (to 61)

18 CALL_FUNCTION 1

21 CALL_FUNCTION 1

24 GET_ITER

25 FOR_ITER 32 (to 60)

28 STORE_FAST 3 (i)

40 BINARY_SUBSCR

47 BINARY_SUBSCR

48 BINARY_SUBTRACT

52 BINARY_POWER

54 STORE_FAST 2 (sum)

57 JUMP_ABSOLUTE 25

60 POP_BLOCK

64 RETURN_VALUE

def test4(u,v):

mysum = 0

for a,v) :

mysum += (a-b)**2

return mysum

dis.dis(test4)

3 STORE_FAST 2 (mysum)

6 SETUP_LOOP 47 (to 56)

18 CALL_FUNCTION 2

21 GET_ITER

22 FOR_ITER 30 (to 55)

25 UNPACK_SEQUENCE 2

28 STORE_FAST 3 (a)

31 STORE_FAST 4 (b)

43 BINARY_SUBTRACT

47 BINARY_POWER

49 STORE_FAST 2 (mysum)

52 JUMP_ABSOLUTE 22

55 POP_BLOCK

59 RETURN_VALUE

12-06 216

03-31 411
03-28 2万+
07-12 441
12-02 841
04-27 319
06-20 6368
03-15 1402
12-27 900