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


Groups > comp.lang.python > #83186

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

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 <rosuav@gmail.com>
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 <d27586b9-017a-4519-a6bb-e0a1fac005d0@googlegroups.com>
References <d27586b9-017a-4519-a6bb-e0a1fac005d0@googlegroups.com>
Date Sun, 4 Jan 2015 10:40:07 +1100
Subject Re: list comparison vs integer comparison, which is more efficient?
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <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 <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.17363.1420328417.18130.python-list@python.org> (permalink)
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

Show key headers only | View raw


On Sun, Jan 4, 2015 at 10:19 AM, austin aigbe <eshikafe@gmail.com> 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

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