Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder1.xlned.com!newsfeed.xs4all.nl!newsfeed2.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; 'url:pypi': 0.03; 'algorithm': 0.04; 'algorithms,': 0.07; 'here?': 0.09; 'performs': 0.09; 'plug': 0.09; 'sake': 0.09; 'subject:number': 0.09; 'worse': 0.09; 'def': 0.12; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'heap': 0.16; 'heapq': 0.16; 'license.': 0.16; 'reinvent': 0.16; 'subject:Prime': 0.16; 'subject:generator': 0.16; 'true:': 0.16; 'yours!': 0.16; ':-)': 0.16; 'wrote:': 0.18; 'thu,': 0.19; 'code,': 0.22; 'import': 0.22; 'finally,': 0.24; 'module,': 0.24; 'sort': 0.25; 'compare': 0.26; 'this:': 0.26; 'least': 0.26; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; '----': 0.29; 'am,': 0.29; 'to?': 0.30; 'message- id:@mail.gmail.com': 0.30; 'code': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'anyone': 0.31; 'url:python': 0.33; "can't": 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'version': 0.36; 'yield': 0.36; "didn't": 0.36; 'similar': 0.36; 'url:org': 0.36; 'question,': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'algorithms': 0.60; "you're": 0.61; 'times': 0.62; 'name': 0.63; 'real': 0.63; 'skip:n 10': 0.64; 'kept': 0.65; 'series': 0.66; 'jul': 0.74; 'prime': 0.74; 'hoping': 0.75; 'low': 0.83; 'benchmark': 0.84; 'have?': 0.84; 'improvement': 0.84; 'wheel': 0.84; '2013': 0.98 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:to :content-type; bh=DXDoHug+nQ/gJ1fbw/iVnUzust/icpLbIldes+XwU7s=; b=0ykd11mswGHMLUJSNLarV7cpkO+ZL9Up8D7jixh5QwcB9vFHpZYCwXdUA4t5+Lk2Tu Yrtqb7+EF90G+KU3MPwhkyePjdsVl70kQp6S/y0f6+QxHa/xHh2EL2XVMFZdLFT46RQ6 2cyQjcSc1qYfCPKxD8eWdjM4Eguhctx2F/SwbGSoAvwfFPxPtg6U4jyTCjMxBqy1xwRc f2G3fQSSMor677KAdHL5EqoyHJ3f1EdftshrYxFUxR5cFdZUNWmGD3KPoyChwaKKw5iw 8NDeEwr8udqL1zVLkceFFd1XowVggqpg3tEEQxqMKhhTO1qYKoBdsVDxVWo8iqjhqbmc 1GVA== MIME-Version: 1.0 X-Received: by 10.58.187.4 with SMTP id fo4mr19554676vec.55.1373475151632; Wed, 10 Jul 2013 09:52:31 -0700 (PDT) In-Reply-To: <51dd8560$0$9505$c3e8da3$5496439d@news.astraweb.com> References: <51dd8560$0$9505$c3e8da3$5496439d@news.astraweb.com> Date: Thu, 11 Jul 2013 02:52:31 +1000 Subject: Re: Prime number generator From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 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: 67 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1373475160 news.xs4all.nl 15972 [2001:888:2000:d::a6]:42627 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:50380 On Thu, Jul 11, 2013 at 2:01 AM, Steven D'Aprano wrote: > On Thu, 11 Jul 2013 00:00:59 +1000, Chris Angelico wrote: >> Thirdly, is there any sort of half-sane benchmark that I >> can compare this code to? And finally, whose wheel did I reinvent here? >> What name would this algorithm have? > > I can't directly answer that question, but I can make a shameless plug > for this: > > https://pypi.python.org/pypi/pyprimes > > If your prime generator performs better than, or worse than, all of those > in the above module, I may have to steal it from you :-) Ha, that's what I was hoping for. My algorithm outperforms several of yours! Look! Rosuav: 0.10868923284942639 awful_primes: 16.55546780386893 naive_primes1: 2.6105180965737276 naive_primes2: 1.358270674066116 trial_division: 0.06926075800136644 turner: 0.5736550315752424 croft: 0.007141969160883832 sieve: 0.01786707528470899 cookbook: 0.014790147909859996 wheel: 0.015050236831779529 Okay, so I outperform only algorithms listed as toys... :| Here's similar figures, going to a higher cutoff (I kept it low for the sake of awful_primes) and using only the algos designed for real use: Rosuav: 2.1318494389650082 croft: 0.11628042111497416 sieve: 0.26868582459502566 cookbook: 0.21551174800149164 wheel: 0.4761577239565362 I didn't seriously think that this would outperform mathematically proven and efficient algorithms, it was just a toy. But at least now I know: It's roughly 20 times slower than a leading algo. And that's after the bas-suggested improvement of using a heap. But hey. If you want the code, you're welcome to it - same with anyone else. Here's the heap version as used in the above timings. MIT license. def primes(): """Generate an infinite series of prime numbers.""" from heapq import heappush,heappop i=2 yield 2 prime=[(2,2)] # Heap while True: smallest, prm = heappop(prime) heappush(prime, (smallest+prm, prm)) if i