Path: csiph.com!usenet.pasdenom.info!gegeweb.org!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.030 X-Spam-Evidence: '*H*': 0.94; '*S*': 0.00; 'bits': 0.09; 'none)': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; 'jan': 0.12; 'comparison:': 0.16; 'dwarfed': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'itertools': 0.16; 'recipes': 0.16; 'subject:which': 0.16; 'tuple': 0.16; 'utterly': 0.16; 'wrote:': 0.18; 'not,': 0.20; 'cc:addr:python.org': 0.22; 'integer': 0.24; 'module,': 0.24; 'replace': 0.24; 'looks': 0.24; 'cc:2**0': 0.24; 'header:In-Reply- To:1': 0.27; 'am,': 0.29; 'subject:list': 0.30; 'message- id:@mail.gmail.com': 0.30; 'comparison': 0.31; 'equivalent.': 0.31; 'skip:c 30': 0.32; 'url:python': 0.33; 'table': 0.34; 'could': 0.34; 'test': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'subject:?': 0.36; 'url:org': 0.36; 'list': 0.37; 'form,': 0.38; 'url:library': 0.38; 'list,': 0.38; 'skip:p 20': 0.39; 'break': 0.61; 'full': 0.61; 'url:3': 0.61; 'simply': 0.61; 'simple': 0.61; 'personal': 0.63; 'subject:more': 0.64; 'more': 0.64; 'between': 0.67; 'skip:r 30': 0.69; "how's": 0.74; '2015': 0.84; 'suspicion': 0.84; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=baJKSfPl3rEzu7mzN+BzFAhBF7FPH97lgl6LkgkBW/k=; b=IZlIm7JjaqtlOxAehcITfaOpayy3iLzFUI+Wld8GuftPnqntsRbjw+cGUKRDmB5meU c/J0nCtgfxW0B88+/A6Xb3dhXnJSL2slxxj5f2YP8mqcUxk2xJqAgTE+QopQSbqTv+Lu JmRfoUneRagz5ZTJEA/8LtCBoq7ylWKoMBjx5O6K4KF/K5JLwF2fUuLcZJ3oi37hclfk 7XMwENNy35LODIDNFX3mmluZT1wfBL6ZyYW9wuyWyFUzyJTChF8fo3O39xxwgjfwHnYI u/ekzHNQqz/OAk1Mi2nI7Fgi4LjX1IPumj5F89ZxjaVdpJq8rAJi/DCAjFkwkyiSNTj1 VKMg== MIME-Version: 1.0 X-Received: by 10.50.171.201 with SMTP id aw9mr4956242igc.2.1420328407784; Sat, 03 Jan 2015 15:40:07 -0800 (PST) In-Reply-To: References: Date: Sun, 4 Jan 2015 10:40:07 +1100 Subject: Re: list comparison vs integer comparison, which is more efficient? From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 41 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1420328417 news.xs4all.nl 2919 [2001:888:2000:d::a6]:42013 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:83186 On Sun, Jan 4, 2015 at 10:19 AM, austin aigbe wrote: > I would like to know which is more efficient to use, between an integer comparison and a list comparison: You can test them with the timeit module, but my personal suspicion is that any difference between them will be utterly and completely dwarfed by all your sqrt(2) calls in the complex constructors. If you break those out, and use a tuple instead of a list, you could write this very simply and tidily: bits = { (0,0): complex(1/math.sqrt(2),1/math.sqrt(2)), (0,1): complex(1/math.sqrt(2),-1/math.sqrt(2)), (1,0): complex(-1/math.sqrt(2),1/math.sqrt(2)), (1,1): complex(-1/math.sqrt(2),-1/math.sqrt(2)), } # QPSK - TS 36.211 V12.2.0, section 7.1.2, Table 7.1.2-1 def mp_qpsk(self): r = [] for i in range(self.nbits/2): bit_pair = self.sbits[i*2:i*2+2] r.append(bits[tuple(bit_pair)]) return r At this point, your loop looks very much like a list comprehension in full form, so you can make a simple conversion: # From itertools recipes # https://docs.python.org/3/library/itertools.html def pairwise(iterable): "s -> (s0,s1), (s1,s2), (s2, s3), ..." a, b = tee(iterable) next(b, None) return zip(a, b) # Replace zip() with izip() for the Python 2 equivalent. def mp_qpsk(self): return [bits[pair] for pair in pairwise(self.sbits)] How's that look? I don't care if it's faster or not, I prefer this form :) ChrisA