Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #83189

Re: list comparison vs integer comparison, which is more efficient?

From Terry Reedy <tjreedy@udel.edu>
Subject Re: list comparison vs integer comparison, which is more efficient?
Date 2015-01-04 02:10 -0500
References <d27586b9-017a-4519-a6bb-e0a1fac005d0@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.17365.1420355468.18130.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

list comparison vs integer comparison, which is more efficient? austin aigbe <eshikafe@gmail.com> - 2015-01-03 15:19 -0800
  Re: list comparison vs integer comparison, which is more efficient? Chris Angelico <rosuav@gmail.com> - 2015-01-04 10:40 +1100
  Re: list comparison vs integer comparison, which is more efficient? Terry Reedy <tjreedy@udel.edu> - 2015-01-04 02:10 -0500
    Re: list comparison vs integer comparison, which is more efficient? austin aigbe <eshikafe@gmail.com> - 2015-01-04 03:20 -0800
      Re: list comparison vs integer comparison, which is more efficient? austin aigbe <eshikafe@gmail.com> - 2015-01-04 04:17 -0800
        Re: list comparison vs integer comparison, which is more efficient? Christian Gollwitzer <auriocus@gmx.de> - 2015-01-04 13:22 +0100
          Re: list comparison vs integer comparison, which is more efficient? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-01-04 12:30 +0000
        Re: list comparison vs integer comparison, which is more efficient? Chris Angelico <rosuav@gmail.com> - 2015-01-04 23:25 +1100
        Re: list comparison vs integer comparison, which is more efficient? Jonas Wielicki <jonas@wielicki.name> - 2015-01-04 13:24 +0100

csiph-web