list comparison vs integer comparison, which is more efficient?

austin aigbe eshikafe at gmail.com
Sun Jan 4 07:17:05 EST 2015


On Sunday, January 4, 2015 12:20:26 PM UTC+1, austin aigbe wrote:
> 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.

Hi Terry,

No difference between the int and list comparison in terms of the number of calls(24) and time (0.004s). Main part is the repeated call to sqrt().

However, it took a shorter time (0.004s) with 24 function calls than your code (0.005s) which took just 13 function calls to execute.

Why is this?

Integer comparison profile result:
>>> p = pstats.Stats('lte_phy_mod.txt')
>>> p.strip_dirs().sort_stats(-1).print_stats()
Sun Jan 04 12:36:32 2015    lte_phy_mod.txt

         24 function calls in 0.004 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.004    0.004    0.004    0.004 lte_phy_layer.py:16(<module>)
        1    0.000    0.000    0.000    0.000 lte_phy_layer.py:20(Scrambling)
        1    0.000    0.000    0.000    0.000 lte_phy_layer.py:276(LayerMapping)

        1    0.000    0.000    0.000    0.000 lte_phy_layer.py:278(Precoding)
        1    0.000    0.000    0.000    0.000 lte_phy_layer.py:280(ResourceElementMapping)
        1    0.000    0.000    0.000    0.000 lte_phy_layer.py:282(OFDMSignalGenerator)
        1    0.000    0.000    0.000    0.000 lte_phy_layer.py:65(Modulation)
        1    0.000    0.000    0.000    0.000 lte_phy_layer.py:71(__init__)
        1    0.000    0.000    0.000    0.000 lte_phy_layer.py:87(mp_qpsk)
        1    0.000    0.000    0.000    0.000 {len}
        8    0.000    0.000    0.000    0.000 {math.sqrt}
        4    0.000    0.000    0.000    0.000 {method 'append' of 'list' objects}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
        1    0.000    0.000    0.000    0.000 {range}


<pstats.Stats instance at 0x028F3F08>
>>>

List comparison:
>>> import pstats
>>> p = pstats.Stats('lte_phy_mod2.txt')
>>> p.strip_dirs().sort_stats(-1).print_stats()
Sun Jan 04 12:57:24 2015    lte_phy_mod2.txt

         24 function calls in 0.004 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.004    0.004    0.004    0.004 lte_phy_layer.py:16(<module>)
        1    0.000    0.000    0.000    0.000 lte_phy_layer.py:20(Scrambling)
        1    0.000    0.000    0.000    0.000 lte_phy_layer.py:276(LayerMapping)

        1    0.000    0.000    0.000    0.000 lte_phy_layer.py:278(Precoding)
        1    0.000    0.000    0.000    0.000 lte_phy_layer.py:280(ResourceElementMapping)
        1    0.000    0.000    0.000    0.000 lte_phy_layer.py:282(OFDMSignalGenerator)
        1    0.000    0.000    0.000    0.000 lte_phy_layer.py:65(Modulation)
        1    0.000    0.000    0.000    0.000 lte_phy_layer.py:71(__init__)
        1    0.000    0.000    0.000    0.000 lte_phy_layer.py:87(mp_qpsk)
        1    0.000    0.000    0.000    0.000 {len}
        8    0.000    0.000    0.000    0.000 {math.sqrt}
        4    0.000    0.000    0.000    0.000 {method 'append' of 'list' objects}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
        1    0.000    0.000    0.000    0.000 {range}


<pstats.Stats instance at 0x025E3418>
>>>


Terry's code:

>>> import pstats
>>> p = pstats.Stats('lte_phy_mod3.txt')
>>> p.strip_dirs().sort_stats(-1).print_stats()
Sun Jan 04 13:04:51 2015    lte_phy_mod3.txt

         13 function calls in 0.005 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.004    0.004    0.005    0.005 lte_phy_layer.py:16(<module>)
        1    0.000    0.000    0.000    0.000 lte_phy_layer.py:20(Scrambling)
        1    0.000    0.000    0.000    0.000 lte_phy_layer.py:285(LayerMapping)

        1    0.000    0.000    0.000    0.000 lte_phy_layer.py:287(Precoding)
        1    0.000    0.000    0.000    0.000 lte_phy_layer.py:289(ResourceElementMapping)
        1    0.000    0.000    0.000    0.000 lte_phy_layer.py:291(OFDMSignalGenerator)
        1    0.000    0.000    0.000    0.000 lte_phy_layer.py:65(Modulation)
        1    0.000    0.000    0.000    0.000 lte_phy_layer.py:71(__init__)
        1    0.000    0.000    0.000    0.000 lte_phy_layer.py:87(mp_qpsk)
        1    0.000    0.000    0.000    0.000 {len}
        1    0.000    0.000    0.000    0.000 {math.sqrt}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
        1    0.000    0.000    0.000    0.000 {range}


<pstats.Stats instance at 0x02783418>
>>>



More information about the Python-list mailing list