443,768 Members | 2,054 Online 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
8 Replies

 P: n/a jzakiya

 P: n/a On Nov 17, 2:10*pm, Arnaud Delobelle

 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, 3a[x1] = a[x2] = Falsea [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 I looked online and in books, but couldn't find a definitive answer tothis.I have an array and set multiple elements to either True or False atone 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] = Falsearray[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 , Tim Chase

 P: n/a On Nov 17, 2:54*pm, cla...@lairds.us (Cameron Laird) wrote: In article , Tim Chase *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 I looked online and in books, but couldn't find a definitive answer tothis.I have an array and set multiple elements to either True or False atone 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. 