Multiple equates

Diez B. Roggisch deets at nospam.web.de
Mon Nov 17 14:25:38 EST 2008


Arnaud Delobelle wrote:

> jzakiya <jzakiya at mail.com> writes:
> 
>> I looked online and in books, but couldn't find a definitive answer to
>> this.
>>
>> I have an array and set multiple elements to either True or False at
>> one time.
>>
>> Question: Which way is faster (or does it matter)?
>>
>> 1)
>>
>> array[x1]=array[x2]=........= array[x10] = \
>> array[x11]=array[x12]=... = array[x20] = \
>> ......
>> ......
>> array[x40]=array[x41]=....= array[x50] = False (or True)
>>
>>
>> 2)
>>
>> array[x1]=array[x2]=........= array[x10] = False
>> array[x11]=array[x12]=... = array[x20] = False
>> ......
>> ......
>> array[x40]=array[x41]=....= array[x50] = False
> 
> It doesn't matter as none of this is valid Python. In Python you have to
> write
> 
> array[x1] = False
> array[x2] = False
> 
> Etc...
> 

No. 


l = range(10)
l[0] = l[1] = 100

And there seems to be a small difference between both versions,
but I can't judge for sure if that favors one of them - yet I tend to think
it's not worth the effort... 

import dis

def a():
    l = range(10)
    l[0] = l[1] = 100

def b():
    l = range(10)
    l[0] = 100
    l[1] = 100

print "------------ a ---------------"
dis.dis(a)
print "------------ b ---------------"
dis.dis(b)

------------ a ---------------
  4           0 LOAD_GLOBAL              0 (range)
              3 LOAD_CONST               1 (10)
              6 CALL_FUNCTION            1
              9 STORE_FAST               0 (l)

  5          12 LOAD_CONST               2 (100)
*            15 DUP_TOP
             16 LOAD_FAST                0 (l)
             19 LOAD_CONST               3 (0)
             22 STORE_SUBSCR
             23 LOAD_FAST                0 (l)
             26 LOAD_CONST               4 (1)
             29 STORE_SUBSCR
             30 LOAD_CONST               0 (None)
             33 RETURN_VALUE
------------ b ---------------
  8           0 LOAD_GLOBAL              0 (range)
              3 LOAD_CONST               1 (10)
              6 CALL_FUNCTION            1
              9 STORE_FAST               0 (l)

  9          12 LOAD_CONST               2 (100)
             15 LOAD_FAST                0 (l)
             18 LOAD_CONST               3 (0)
             21 STORE_SUBSCR

 10          22 LOAD_CONST               2 (100)
             25 LOAD_FAST                0 (l)
             28 LOAD_CONST               4 (1)
             31 STORE_SUBSCR
             32 LOAD_CONST               0 (None)
             35 RETURN_VALUE









More information about the Python-list mailing list