[Tutor] List Splicing

Wayne srilyk at gmail.com
Thu Jun 18 02:26:30 CEST 2009


On Wed, Jun 17, 2009 at 7:11 PM, Robert Berman <bermanrl at cfl.rr.com> wrote:

>  Emille,
>
> Thank you for the example of list splicing.  Do you know if this is  faster
> than a more conventional loop statement as in my code for primearray which
> is in my original post (reprinted here)
>
> The code is as follows:
>
> def BuildSieve(itemsin):
>     TheSieve=list()
>     TheSieve = range(0,itemsin+1)
>     TheSieve[1]=0
>     for i in range(2,itemsin+1):
>         if (TheSieve[i] > 0):
>             j = i + i
>             while (j <= itemsin):
>                 TheSieve[j] = 0
>                 j+=i
>     return TheSieve
>
> It is called with PrimaryList = BuildSieve(1000000)
>

I'm curious if it wouldn't be faster to use xrange. I know for certain
instead of range(0, itemsin+1) you could use TheSieve = [1, 2]
TheSieve.extend(range(3, itemsin+1,2))

Which would save you at least half the time in your generation. Since your
first step should be removing multiples of two, that should be a big help.

HTH,
Wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090617/a32e365c/attachment.htm>


More information about the Tutor mailing list