Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.029 X-Spam-Evidence: '*H*': 0.94; '*S*': 0.00; 'exercise': 0.04; 'method.': 0.07; 'sys': 0.07; 'builtin': 0.09; 'python': 0.11; '(given': 0.16; '(say': 0.16; 'benjamin': 0.16; 'boolean': 0.16; 'inclined': 0.16; 'lists).': 0.16; 'numpy': 0.16; 'slice.': 0.16; 'subclass': 0.16; 'elements': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'items.': 0.19; 'seems': 0.21; '>>>': 0.22; 'memory': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'byte': 0.24; 'decide': 0.24; 'mon,': 0.24; 'regardless': 0.24; 'extension': 0.26; 'possibly': 0.26; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'function': 0.29; 'wondering': 0.29; 'array': 0.29; 'compared': 0.30; 'fastest': 0.30; '+0100,': 0.31; '>>>>': 0.31; "d'aprano": 0.31; 'factor': 0.31; 'libraries': 0.31; 'overhead': 0.31; 'steven': 0.31; 'this.': 0.32; 'worked': 0.33; 'becomes': 0.33; 'basic': 0.35; 'advice': 0.35; 'agree': 0.35; 'hundreds': 0.35; 'operations': 0.35; 'but': 0.35; 'add': 0.35; 'building': 0.35; 'there': 0.35; 'really': 0.36; 'grateful': 0.36; 'subject:List': 0.36; 'useful': 0.36; 'thanks': 0.36; "i'll": 0.36; 'list': 0.37; 'list.': 0.37; 'to:addr:python-list': 0.38; 'issue': 0.38; 'expect': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'enough': 0.39; 'system.': 0.39; 'easy': 0.60; 'full': 0.61; 'simple': 0.61; 'offer': 0.62; 'high': 0.63; 'believe': 0.68; 'price': 0.69; 'allocation': 0.74; 'lack': 0.78; 'costly': 0.84; 'oscar': 0.84; 'received:192.168.1.7': 0.84; 'items,': 0.91; 'system:': 0.91; '30%': 0.93; 'suffer': 0.93; '2013': 0.98 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=YbCEuWhf c=1 sm=1 tr=0 a=nO6JWm9Dsne7RoCvRGnC0w==:117 a=nO6JWm9Dsne7RoCvRGnC0w==:17 a=0Bzu9jTXAAAA:8 a=fr8gcSRtuOIA:10 a=IVHPnoOXeGEA:10 a=8nJEP1OIZ-IA:10 a=BRpsHpjDAAAA:8 a=sXOIDwgukokA:10 a=c38MiZz4vbaADgMbrOsA:9 a=wPNLvfGTeEIA:10 a=VMCyCadrP7MA:10 X-AUTH: gladman+brg:2500 Date: Mon, 22 Apr 2013 16:50:16 +0100 From: Blind Anagram User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20130328 Thunderbird/17.0.5 MIME-Version: 1.0 Newsgroups: comp.lang.python To: python-list@python.org Subject: Re: List Count References: <5175377f$0$29977$c3e8da3$5496439d@news.astraweb.com> <517545F7.5090209@nowhere.org> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Message-ID: Lines: 64 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1366645838 news.xs4all.nl 2178 [2001:888:2000:d::a6]:37945 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:44091 On 22/04/2013 16:14, Oscar Benjamin wrote: > On 22 April 2013 15:15, Blind Anagram wrote: >> On 22/04/2013 14:13, Steven D'Aprano wrote: >>> On Mon, 22 Apr 2013 12:58:20 +0100, Blind Anagram wrote: >>> >>>> I would be grateful for any advice people can offer on the fastest way >>>> to count items in a sub-sequence of a large list. >>>> >>>> I have a list of boolean values that can contain many hundreds of >>>> millions of elements for which I want to count the number of True values >>>> in a sub-sequence, one from the start up to some value (say hi). >>>> >>>> I am currently using: >>>> >>>> sieve[:hi].count(True) >>>> >>>> but I believe this may be costly because it copies a possibly large part >>>> of the sieve. > [snip] >> >> But when using a sub-sequence, I do suffer a significant reduction in >> speed for a count when compared with count on the full list. When the >> list is small enough not to cause memory allocation issues this is about >> 30% on 100,000,000 items. But when the list is 1,000,000,000 items, OS >> memory allocation becomes an issue and the cost on my system rises to >> over 600%. > > Have you tried using numpy? I find that it reduces the memory required > to store a list of bools by a factor of 4 on my 32 bit system. I would > expect that to be a factor of 8 on a 64 bit system: > >>>> import sys >>>> a = [True] * 1000000 >>>> sys.getsizeof(a) > 4000036 >>>> import numpy >>>> a = numpy.ndarray(1000000, bool) >>>> sys.getsizeof(a) # This does not include the data buffer > 40 >>>> a.nbytes > 1000000 > > The numpy array also has the advantage that slicing does not actually > copy the data (as has already been mentioned). On this system slicing > a numpy array has a 40 byte overhead regardless of the size of the > slice. > >> I agree that this is not a big issue but it seems to me a high price to >> pay for the lack of a sieve.count(value, limit), which I feel is a >> useful function (given that memoryview operations are not available for >> lists). > > It would be very easy to subclass list and add this functionality in > cython if you decide that you do need a builtin method. Thanks Oscar, I'll take a look at this. But I was really wondering if there was a simple solution that worked without people having to add libraries to their basic Python installations. As I have never tried building an extension with cython, I am inclined to try this as a learning exercise if nothing else.