Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'elif': 0.05; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'repeated': 0.09; 'python': 0.11; 'def': 0.12; 'jan': 0.12; 'question.': 0.14; '2)]': 0.16; 'comparison:': 0.16; 'range(0,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'subject:which': 0.16; 'wrote:': 0.18; 'implementing': 0.19; 'header:User-Agent:1': 0.23; 'integer': 0.24; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'points': 0.29; 'subject:list': 0.30; 'comparison': 0.31; 'factor': 0.31; 'layer': 0.31; 'skip:c 30': 0.32; 'table': 0.34; 'subject:?': 0.36; 'wrong': 0.37; 'list': 0.37; 'skip:[ 10': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'subject:more': 0.64; 'more': 0.64; 'between': 0.67; 'skip:r 40': 0.68; 'physical': 0.72; 'received:fios.verizon.net': 0.84; 'received:108': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: list comparison vs integer comparison, which is more efficient? Date: Sun, 04 Jan 2015 02:10:41 -0500 References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-108-16-203-145.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.3.0 In-Reply-To: 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: 55 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1420355468 news.xs4all.nl 2901 [2001:888:2000:d::a6]:57837 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:83189 On 1/3/2015 6:19 PM, austin aigbe wrote: > I am currently implementing the LTE physical layer in Python (ver 2.7.7). > For the qpsk, 16qam and 64qam modulation I would like to know which is more efficient to use, between an integer comparison and a list comparison: > > Integer comparison: bit_pair as an integer value before comparison > > # 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] << 1) | self.sbits[i*2+1] > if bit_pair == 0: > r.append(complex(1/math.sqrt(2),1/math.sqrt(2))) > elif bit_pair == 1: > r.append(complex(1/math.sqrt(2),-1/math.sqrt(2))) > elif bit_pair == 2: > r.append(complex(-1/math.sqrt(2),1/math.sqrt(2))) > elif bit_pair == 3: > r.append(complex(-1/math.sqrt(2),-1/math.sqrt(2))) > return r > > List comparison: bit_pair as a list before comparison > > # 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] > if bit_pair == [0,0]: > r.append() > elif bit_pair == [0,1]: > r.append(complex(1/math.sqrt(2),-1/math.sqrt(2))) > elif bit_pair == [1,0]: > r.append(complex(-1/math.sqrt(2),1/math.sqrt(2))) > elif bit_pair == [1,1]: > r.append(complex(-1/math.sqrt(2),-1/math.sqrt(2))) > return r Wrong question. If you are worried about efficiency, factor out all repeated calculation of constants and eliminate the multiple comparisons. sbits = self.sbits a = 1.0 / math.sqrt(2) b = -a points = (complex(a,a), complex(a,b), complex(b,a), complex(b,b)) complex(math.sqrt(2),1/math.sqrt(2)) def mp_qpsk(self): r = [points[sbits[i]*2 + sbits[i+1]] for i in range(0, self.nbits, 2)] return r -- Terry Jan Reedy