[Tutor] List Splicing
Robert Berman
bermanrl at cfl.rr.com
Thu Jun 18 02:33:07 CEST 2009
Wayne,
Thank you for the suggestion. I will let you know how well this plays
out.
Robert
On Wed, 2009-06-17 at 19:26 -0500, Wayne wrote:
> 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
>
>
More information about the Tutor
mailing list