list comparison vs integer comparison, which is more efficient?

austin aigbe eshikafe at gmail.com
Sun Jan 4 06:20:09 EST 2015


On Sunday, January 4, 2015 8:12:10 AM UTC+1, Terry Reedy wrote:
> 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

Cool. Thanks a lot.



More information about the Python-list mailing list