Path: csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!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.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; '"""': 0.07; 'assuming': 0.09; 'skip:# 30': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'random': 0.14; '--prefix': 0.16; 'bit.': 0.16; 'fiddle': 0.16; 'from:addr:pobox.com': 0.16; 'from:addr:skip': 0.16; 'itertools': 0.16; 'numpy': 0.16; 'unsigned': 0.16; 'accordingly.': 0.16; 'sender:addr:gmail.com': 0.17; 'not,': 0.20; 'import': 0.22; 'cc:addr:python.org': 0.22; 'install': 0.23; 'bytes': 0.24; 'directory.': 0.24; 'skip': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'wondering': 0.29; 'skip:p 30': 0.29; 'array': 0.29; "doesn't": 0.30; 'forgot': 0.30; 'message-id:@mail.gmail.com': 0.30; 'libraries': 0.31; 'though.': 0.31; 'stuff': 0.32; 'worked': 0.33; 'basic': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'there': 0.35; 'really': 0.36; 'installing': 0.36; 'module.': 0.36; 'subject:List': 0.36; 'performance': 0.37; 'skip:[ 10': 0.38; 'days': 0.60; 'skip:t 30': 0.61; 'simple': 0.61; 'skip:n 10': 0.64; 'provide': 0.64; 'more': 0.64; 'love': 0.65; 'imagine': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type; bh=4Va0id8fwN0oDUqz5h4ix5W2b5X3VDZ0QZT4YZ9CSgw=; b=ErPZ0l84M7ava8BRNuw5cTh0aYSUm67R7uwwmQEprittD0g6yU0V3IORaFhhpG/ENT 4A9jKiezfH9DRd9C16U0e+IbRzdDudVSYWirIeITyHrnKy6QCL3JqQ1RhtwknzFe5zcw dovVWOA8HK2pondfwFhJgHfVr9HExE57ZtR5zlGFGjS0Re/SVxGsHt0BE6+GGjxLnqCx x0vMyZx7Qt7ljxmG9v+xMunyA49m1k+e0gYV3K2od85EZNDrjsJiAc9d0ajD0IZEkKRS nfIyAfu8hFxHAb+j+VrKM/U0E0uVCG4HnS0+jkjUzERKCc1hn4iTq8f0U9wtOAUPwMfH pXZg== MIME-Version: 1.0 X-Received: by 10.50.130.83 with SMTP id oc19mr16303616igb.29.1366652891176; Mon, 22 Apr 2013 10:48:11 -0700 (PDT) Sender: skip.montanaro@gmail.com In-Reply-To: <51756769.20206@nowhere.org> References: <5175377f$0$29977$c3e8da3$5496439d@news.astraweb.com> <517545F7.5090209@nowhere.org> <51755C38.4000204@nowhere.org> <51756769.20206@nowhere.org> Date: Mon, 22 Apr 2013 12:48:10 -0500 X-Google-Sender-Auth: OYB4NlA2QCwkhIYoAxpQJ2nbFhY Subject: Re: List Count From: Skip Montanaro To: Blind Anagram Content-Type: text/plain; charset=UTF-8 Cc: python-list@python.org 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: 53 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1366652894 news.xs4all.nl 2174 [2001:888:2000:d::a6]:51755 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:44101 > But I was really wondering if there was a simple solution that worked > without people having to add libraries to their basic Python installations. I think installing numpy is approximately pip install numpy assuming you have write access to your site-packages directory. If not, install using --prefix and set PYTHONPATH accordingly. I forgot that Python also has an array module. With numpy available, mature, and well-supported, I imagine it doesn't get much love these days though. Still, I gave it a whirl: ####################################### import random import array from timeit import Timer import numpy stuff = [random.random() < 0.5 for i in range(10**7)] sieve1 = numpy.array(stuff, dtype=bool) sieve2 = array.array('B', stuff) setup = """from __main__ import sieve1, sieve2 from itertools import islice hi = 7*10**6 """ t1 = Timer("(True == sieve1[:hi]).sum()", setup) t2 = Timer("sieve2[:hi].count(True)", setup) # t3 = Timer("sum(islice(sieve, hi))", setup) # t4 = Timer("sum(x for x in islice(sieve, hi) if x)", setup) # t5 = Timer("sum(x for x in islice(sieve, hi) if x is True)", setup) # t6 = Timer("sum(1 for x in islice(sieve, hi) if x is True)", setup) # t7 = Timer("len(list(filter(None, islice(sieve, hi))))", setup) print(min(t1.repeat(number=10))) print(min(t2.repeat(number=10))) # for t in (t1, t2, t3, t4, t5, t6, t7): # print( min(t.repeat(number=10)) ) ####################################### Performance was not all that impressive: 0.340315103531 5.42102503777 Still, you might fiddle around with it a bit. Perhaps unsigned ints instead of unsigned bytes will provide more efficient counting... Skip