By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,768 Members | 2,054 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,768 IT Pros & Developers. It's quick & easy.

Multiple equates

P: n/a
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

Nov 17 '08 #1
Share this Question
Share on Google+
8 Replies


P: n/a
jzakiya <jz*****@mail.comwrites:
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
Nov 17 '08 #2

P: n/a
On Nov 17, 2:10*pm, Arnaud Delobelle <arno...@googlemail.comwrote:
jzakiya <jzak...@mail.comwrites:
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
Could you explain please.
Python allows this, so is it just not considered good idiomatic
python?

jz
Nov 17 '08 #3

P: n/a
Arnaud Delobelle wrote:
jzakiya <jz*****@mail.comwrites:
>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


Nov 17 '08 #4

P: n/a
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

Nov 17 '08 #5

P: n/a
Arnaud Delobelle <ar*****@googlemail.comwrites:
jzakiya <jz*****@mail.comwrites:
>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
Nov 17 '08 #6

P: n/a
In article <ma**************************************@python.o rg>,
Tim Chase <py*********@tim.thechases.comwrote:
Nov 17 '08 #7

P: n/a
On Nov 17, 2:54*pm, cla...@lairds.us (Cameron Laird) wrote:
In article <mailman.4155.1226950874.3487.python-l...@python.org>,
Tim Chase *<python.l...@tim.thechases.comwrote:
* * * * * * * * * * * * .
* * * * * * * * * * * * .
* * * * * * * * * * * * .>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])

My answer: *no.
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
Nov 18 '08 #8

P: n/a
jzakiya <jz*****@mail.comwrote:
>
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, ti**@probo.com
Providenza & Boekelheide, Inc.
Nov 20 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.