Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder7.xlned.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'output': 0.05; '"""': 0.07; 'python': 0.11; '2.7': 0.14; 'random': 0.14; 'array.': 0.16; 'from:addr:pobox.com': 0.16; 'from:addr:skip': 0.16; 'itertools': 0.16; 'numpy': 0.16; 'skip:# 20': 0.16; 'sender:addr:gmail.com': 0.17; 'import': 0.22; 'skip': 0.24; 'this:': 0.26; 'header:In- Reply-To:1': 0.27; 'skip:p 30': 0.29; "doesn't": 0.30; 'message- id:@mail.gmail.com': 0.30; 'skip:7 10': 0.31; 'run': 0.32; 'objects': 0.35; 'received:google.com': 0.35; 'subject:List': 0.36; 'skip:4 10': 0.37; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'space': 0.40; 'idiom': 0.84; 'improvement': 0.84 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:content-type; bh=MOLYy+I2xpxf/YnMzt7uTWF7tCMEFA4LL+q37CmMZ8E=; b=mZt/y5ssgniaV6kxh0ap/utNTvA5hd0AJxZvv+nA5MNMpBo/bov8dDRfN0UtWw8B1U 0nMGtNcbUMpL0XxkJunafy8X99uMtBCRDuR6cUCXmTalQc73HCHxTnNLxRnUsfpGHpPd 6tBLt38BNbWUMTbCkzJdNZANREwWll7+TESycuBx+MJksCSG0bQxaaOIuIaVcc1m+4Ji Dn3QSOO3dNybC2tXd+PvhZk/hFEY3BqynUFygP8NbkoZK8qiQxm8cvFw25sR4h8H1XDG L+oSRlBnD/RrMEAf60dZUfED8dRSsMO3f4OYq3lxyKK0d1SuvGebgD/OuC+QjxHAHv5C e2cg== MIME-Version: 1.0 X-Received: by 10.50.37.212 with SMTP id a20mr15590408igk.88.1366639046872; Mon, 22 Apr 2013 06:57:26 -0700 (PDT) Sender: skip.montanaro@gmail.com In-Reply-To: <5175377f$0$29977$c3e8da3$5496439d@news.astraweb.com> References: <5175377f$0$29977$c3e8da3$5496439d@news.astraweb.com> Date: Mon, 22 Apr 2013 08:57:26 -0500 X-Google-Sender-Auth: PxKkKMd8SywiahPa8Zk7JKNiE3Q Subject: Re: List Count From: Skip Montanaro To: 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 40 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1366639056 news.xs4all.nl 2227 [2001:888:2000:d::a6]:53609 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:44071 Numpy is a big improvement here. In Py 2.7 I get this output if I run Steven's benchmark: 2.10364603996 3.68471002579 4.01849389076 7.41974878311 10.4202470779 9.16782712936 3.36137390137 (confirming his results). If I then run the numpy idiom for this: ######################## import random from timeit import Timer import numpy sieve = numpy.array([random.random() < 0.5 for i in range(10**7)], dtype=bool) setup = """from __main__ import sieve from itertools import islice hi = 7*10**6 """ t1 = Timer("(True == sieve[:hi]).sum()", setup) print(min(t1.repeat(number=10))) ########################### I get : 0.344316959381 It likely consumes less space as well, since it doesn't store Python objects in the array. Skip