On 22/04/2013 18:48, Skip Montanaro wrote: >> But I was really wondering if there was a simple solution that worked >> without people having to add libraries to their basic Python installations. > > I think installing numpy is approximately > > pip install numpy > > assuming you have write access to your site-packages directory. If > not, install using --prefix and set PYTHONPATH accordingly. > > I forgot that Python also has an array module. With numpy available, > mature, and well-supported, I imagine it doesn't get much love these > days though. Still, I gave it a whirl: > > ####################################### > import random > import array > from timeit import Timer > > import numpy > > stuff = [random.random() < 0.5 for i in range(10**7)] > sieve1 = numpy.array(stuff, dtype=bool) > sieve2 = array.array('B', stuff) > > setup = """from __main__ import sieve1, sieve2 > from itertools import islice > hi = 7*10**6 > """ > > t1 = Timer("(True == sieve1[:hi]).sum()", setup) > t2 = Timer("sieve2[:hi].count(True)", setup) > # t3 = Timer("sum(islice(sieve, hi))", setup) > # t4 = Timer("sum(x for x in islice(sieve, hi) if x)", setup) > # t5 = Timer("sum(x for x in islice(sieve, hi) if x is True)", setup) > # t6 = Timer("sum(1 for x in islice(sieve, hi) if x is True)", setup) > # t7 = Timer("len(list(filter(None, islice(sieve, hi))))", setup) > > print(min(t1.repeat(number=10))) > print(min(t2.repeat(number=10))) > # for t in (t1, t2, t3, t4, t5, t6, t7): > # print( min(t.repeat(number=10)) ) > ####################################### > > Performance was not all that impressive: > > 0.340315103531 > 5.42102503777 > > Still, you might fiddle around with it a bit. Perhaps unsigned ints > instead of unsigned bytes will provide more efficient counting...
I spent a lot of time comparing python arrays and lists but found that lists were always much faster in this application. I do have numpy installed but I remember that when I did this (some time ago) it was far from easy with Python 3.x running natively on Windows x64. Brian -- http://mail.python.org/mailman/listinfo/python-list