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


Groups > comp.lang.python > #89585

Re: Lucky numbers in Python

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!lightspeed.eweka.nl!lightspeed.eweka.nl!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ckaynor@zindagigames.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.050
X-Spam-Evidence '*H*': 0.90; '*S*': 0.00; 'algorithm': 0.04; 'subject:Python': 0.06; 'element': 0.07; 'wrapped': 0.09; 'times,': 0.14; '&gt;&gt;': 0.16; 'efficiently,': 0.16; 'iteration': 0.16; 'removed,': 0.16; 'elements': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'to:name:python-list@python.org': 0.22; '&gt;': 0.26; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'wondering': 0.29; 'chris': 0.29; 'message-id:@mail.gmail.com': 0.30; 'moderate': 0.31; 'subject:numbers': 0.31; 'run': 0.32; 'received:74.125.82': 0.34; 'could': 0.34; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'done': 0.36; 'should': 0.36; 'seconds': 0.37; 'too': 0.37; 'list': 0.37; 'performance': 0.37; 'expected': 0.38; 'to:addr:python-list': 0.38; 'skip:- 10': 0.38; 'pm,': 0.38; 'rather': 0.38; 'expect': 0.39; 'received:74.125': 0.39; 'delete': 0.39; 'to:addr:python.org': 0.39; 'deleting': 0.60; 'new': 0.61; 'took': 0.61; 'times': 0.62; 'such': 0.63; 'more': 0.64; 'situation': 0.65; 'overall': 0.69; '2015': 0.84; 'delete,': 0.84; 'hundred': 0.95
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:content-type; bh=v1ALmUvH33BMOYNrfnWFrLiH+IRoips3x2CaaITECiY=; b=Jh1GVUhczCEEisjgZujsW9X4ICPIeHQsma/k2AH4u9KqfSgeXepSPKmLYBni9Y0aLg htu7xUhazIjiaEsa37Ug14umCAW8zZsIgG+ibt9Fm69wL/XzIUPgIDM/DBkfTB2ZEpGc QI63qnNjhg3hWTyxhPzv0aRaHN1cvOAy0H12RyerT0EoAB7dWRSnMug3hNO7ADhpb6GT JQtWBesSvMMIrSgH+nXVemP6vonzePx+hzbuLJv+Evs+XJ/hzw4cddBto9+qzg03nr51 o2IX+33QMrF7Q9lBuqHpTL88VC4IJEuhe04e4CIgT6J+/86AMU5MQFwiSKdHIx406oKk G9Rw==
X-Gm-Message-State ALoCoQkT4WuPW7hhqE/yl30sNlFV/rzuicBXeZnWG8sS+i7TIkbrgvgVHWf7k/ZZtehwNd6d7DCT
X-Received by 10.194.190.49 with SMTP id gn17mr2668192wjc.11.1430348204683; Wed, 29 Apr 2015 15:56:44 -0700 (PDT)
MIME-Version 1.0
In-Reply-To <87d22mbod7.fsf@Equus.decebal.nl>
References <87lhhabxod.fsf@Equus.decebal.nl> <mailman.95.1430337506.3680.python-list@python.org> <87d22mbod7.fsf@Equus.decebal.nl>
From Chris Kaynor <ckaynor@zindagigames.com>
Date Wed, 29 Apr 2015 15:56:24 -0700
Subject Re: Lucky numbers in Python
To "python-list@python.org" <python-list@python.org>
Content-Type multipart/alternative; boundary=047d7bf0c1b4a54d510514e4e4bc
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
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.104.1430348212.3680.python-list@python.org> (permalink)
Lines 88
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1430348212 news.xs4all.nl 2948 [2001:888:2000:d::a6]:44220
X-Complaints-To abuse@xs4all.nl
X-Received-Bytes 7466
X-Received-Body-CRC 3523360318
Xref csiph.com comp.lang.python:89585

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

On Wed, Apr 29, 2015 at 2:45 PM, Cecil Westerhof <Cecil@decebal.nl> wrote:

> >> I was wondering if there is a way to do this:
> >> for del_index in range((sieve_len // skip_count) * skip_count - 1,
> >> skip_count - 2, -skip_count):
> >> del sieve[del_index]
> >> in a more efficient way.
> >
> > You can delete using slices.
> >
> > del sieve[(sieve_len // skip_count) * skip_count - 1 : skip_count -
> > 2 : -skip_count]
> >
> > Now you no longer need to do the iteration in reverse, which makes
> > the slicing simpler:
> >
> > del sieve[skip_count - 1 : (sieve_len // skip_count) * skip_count :
> > skip_count]
>
> I expected that it could be done more efficiently, but did not expect
> such a big difference: more as hundred times. The old situation took
> 20 seconds for 1000000. The new takes 0.17.


Its not too surprising, as deleting the non-end element of a list is a O(n)
operation - it must copy all elements in the list into a new list each
time. This means that your algorithm is roughly O(n*n*log(n)) performance -
n for each list delete, which is wrapped in a for loop of n iterations,
which is wrapped in a while loop which will run log(n) times (I think that
while loop will run log(n) times, but have not actually tested the math).

Deleting a slice should take n time as well, however it is now done only
once rather than once per item to be removed, which should reduce the
overall algorithm to O(n*log(n)) time - aka, a HUGE difference with any
moderate to large input.

Chris

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Lucky numbers in Python Cecil Westerhof <Cecil@decebal.nl> - 2015-04-29 20:24 +0200
  Re: Lucky numbers in Python Ian Kelly <ian.g.kelly@gmail.com> - 2015-04-29 13:57 -0600
    Re: Lucky numbers in Python Cecil Westerhof <Cecil@decebal.nl> - 2015-04-29 23:45 +0200
      Re: Lucky numbers in Python Ian Kelly <ian.g.kelly@gmail.com> - 2015-04-29 16:38 -0600
        Re: Lucky numbers in Python Cecil Westerhof <Cecil@decebal.nl> - 2015-04-30 02:01 +0200
          Re: Lucky numbers in Python Ian Kelly <ian.g.kelly@gmail.com> - 2015-04-29 20:55 -0600
            Re: Lucky numbers in Python Cecil Westerhof <Cecil@decebal.nl> - 2015-04-30 08:34 +0200
      Re: Lucky numbers in Python Chris Kaynor <ckaynor@zindagigames.com> - 2015-04-29 15:56 -0700
    Re: Lucky numbers in Python Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-04-30 10:11 +1000
      Re: Lucky numbers in Python Ian Kelly <ian.g.kelly@gmail.com> - 2015-04-29 21:08 -0600
  Re: Lucky numbers in Python Cecil Westerhof <Cecil@decebal.nl> - 2015-04-30 20:55 +0200
    Re: Lucky numbers in Python Dave Angel <davea@davea.name> - 2015-04-30 15:28 -0400

csiph-web