List Count

Skip Montanaro skip at pobox.com
Mon Apr 22 09:57:26 EDT 2013


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



More information about the Python-list mailing list