Velocity Reviews > Multiple equates

# Multiple equates

jzakiya
Guest
Posts: n/a

 11-17-2008
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

Arnaud Delobelle
Guest
Posts: n/a

 11-17-2008
jzakiya <(E-Mail Removed)> 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...

--
Arnaud

jzakiya
Guest
Posts: n/a

 11-17-2008
On Nov 17, 2:10*pm, Arnaud Delobelle <(E-Mail Removed)> wrote:
> jzakiya <(E-Mail Removed)> 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...
>
> --
> Arnaud

Python allows this, so is it just not considered good idiomatic
python?

jz

Diez B. Roggisch
Guest
Posts: n/a

 11-17-2008
Arnaud Delobelle wrote:

> jzakiya <(E-Mail Removed)> 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 ---------------
6 CALL_FUNCTION 1
9 STORE_FAST 0 (l)

* 15 DUP_TOP
22 STORE_SUBSCR
29 STORE_SUBSCR
33 RETURN_VALUE
------------ b ---------------
6 CALL_FUNCTION 1
9 STORE_FAST 0 (l)

21 STORE_SUBSCR

31 STORE_SUBSCR
35 RETURN_VALUE

Tim Chase
Guest
Posts: n/a

 11-17-2008
> It doesn't matter as none of this is valid Python. In Python you have to
> write
>
> array[x1] = False
> array[x2] = False

Uh...not so much...

>>> a = [1,2,3,4,5]
>>> x1, x2 = 1, 3
>>> a[x1] = a[x2] = False
>>> a

[1, False, 3, False, 5]

Works for me.

To the OP, I think rather than cluttering my code, I'd just
create a loop

for i in [x1,x2,x3,x4,...x1024]:
a[i] = False

From Diez's disassembly of it (as an aside, nifty little intro
to dis.dis()...thanks, Diez!), it looks like it boils down to "is
DUP_TOP faster than LOAD_CONST" because the rest of the
operations. At this point, it's pretty nitty-gritty.

Unless the code is in an inner loop somewhere, the simple loop
should be more than fast enough. Without knowing the source of
the [x1,...] index variables, it's hard to tell if there's a more
optimal way to do this.

-tkc

Arnaud Delobelle
Guest
Posts: n/a

 11-17-2008
Arnaud Delobelle <(E-Mail Removed)> writes:

> jzakiya <(E-Mail Removed)> 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...

Sorry, I don't know what came over me then, some kind of brain
short-circuit... No doubt plenty of people will correct me before this
is posted!

Of course it's valid Python, but it's not very stylish. Assuming your
array is a list, you could put all your indices that are meant to be
True in a list:

indices = [x1, x2, x3, ..., x50]

Then loop over them:

for i in indices:
array[i] = True

It might not be as fast of course

--
Arnaud

Cameron Laird
Guest
Posts: n/a

 11-17-2008
In article <(E-Mail Removed)>,
Tim Chase <(E-Mail Removed)> wrote:
.
.
.
>To the OP, I think rather than cluttering my code, I'd just
>create a loop
>
> for i in [x1,x2,x3,x4,...x1024]:
> a[i] = False

.
.
.
I much prefer this, too.

I've been trying to decide if there's any sober reason to advocate
the one-liner

map(lambda i: a.__setitem__(i, False), [x1, x2, x3, ..., x1024])

CarlFK
Guest
Posts: n/a

 11-18-2008
On Nov 17, 2:54*pm, (E-Mail Removed) (Cameron Laird) wrote:
> In article <(E-Mail Removed)>,
> Tim Chase *<(E-Mail Removed)> wrote:
> * * * * * * * * * * * * .
> * * * * * * * * * * * * .
> * * * * * * * * * * * * .>To the OP, I think rather than cluttering my code, I'd just
> >create a loop

>
> > * for i in [x1,x2,x3,x4,...x1024]:
> > * * a[i] = False

>
> * * * * * * * * * * * * .
> * * * * * * * * * * * * .
> * * * * * * * * * * * * .
> I much prefer this, too.
>
> I've been trying to decide if there's any sober reason to advocate
> the one-liner
>
> * * map(lambda i: a.__setitem__(i, False), [x1, x2, x3, ..., x1024])
>

well, the OP was "...faster?" not "best, cleanest, pythonic..." so
the reason would be speed.

(looks faster, don't know for sure)
(and yeah, looks ugly. but we need the speed...)

Carl K

Tim Roberts
Guest
Posts: n/a

 11-20-2008
jzakiya <(E-Mail Removed)> wrote:
>
>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)?

Answer: it does not matter. This is premature optimization. First, make
it work. Then, figure out whether it is fast enough. THEN, figure out
what's taking the most time.

I'd be very, very surprised if this was an important part of your run time.
--
Tim Roberts, http://www.velocityreviews.com/forums/(E-Mail Removed)
Providenza & Boekelheide, Inc.

Lawrence D'Oliveiro
Guest
Posts: n/a

 11-25-2008
Cameron Laird wrote:

> I've been trying to decide if there's any sober reason to advocate
> the one-liner
>
> map(lambda i: a.__setitem__(i, False), [x1, x2, x3, ..., x1024])

Are lambdas like the Dark Side of Python?

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Mike Copeland C++ 14 11-29-2011 12:10 PM XP NZ Computing 8 09-17-2005 03:55 AM Chennak Cisco 10 06-08-2005 09:29 PM Scotchy Cisco 2 10-07-2004 03:35 PM Calvin Perl 2 08-01-2004 08:55 PM