Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #44132

Re: List Count

Date 2013-04-22 21:47 -0400
From Dave Angel <davea@davea.name>
Subject Re: List Count
References (5 earlier) <mailman.929.1366646795.3114.python-list@python.org> <51756769.20206@nowhere.org> <CAHVvXxSVt843jVvoXv1J8zD2Ghgdu2taPnr0vawVPCQR=cePtA@mail.gmail.com> <mailman.938.1366664643.3114.python-list@python.org> <MOmdnVeRh4PiMejMnZ2dnUVZ7t6dnZ2d@brightview.co.uk>
Newsgroups comp.lang.python
Message-ID <mailman.946.1366681733.3114.python-list@python.org> (permalink)

Show all headers | View raw


On 04/22/2013 05:32 PM, Blind Anagram wrote:
> On 22/04/2013 22:03, Oscar Benjamin wrote:
>> On 22 April 2013 21:18, Oscar Benjamin <oscar.j.benjamin@gmail.com> wrote:
>>> On 22 April 2013 17:38, Blind Anagram <blindanagram@nowhere.org> wrote:
>>>> On 22/04/2013 17:06, Oscar Benjamin wrote:
>>>>
>>>>> I don't know what your application is but I would say that my first
>>>>> port of call here would be to consider a different algorithmic
>>>>> approach. An obvious question would be about the sparsity of this data
>>>>> structure. How frequent are the values that you are trying to count?
>>>>> Would it make more sense to store a list of their indices?
>>>>
>>>> Actually it is no more than a simple prime sieve implemented as a Python
>>>> class (and, yes, I realize that there are plenty of these around).
>>>
>>> If I understand correctly, you have a list of roughly a billion
>>> True/False values indicating which integers are prime and which are
>>> not. You would like to discover how many prime numbers there are
>>> between two numbers a and b. You currently do this by counting the
>>> number of True values in your list between the indices a and b.
>>>
>>> If my description is correct then I would definitely consider using a
>>> different algorithmic approach. The density of primes from 1 to 1
>>> billlion is about 5%. Storing the prime numbers themselves in a sorted
>>> list would save memory and allow a potentially more efficient way of
>>> counting the number of primes within some interval.
>>
>> In fact it is probably quicker if you don't mind using all that memory
>> to just store the cumulative sum of your prime True/False indicator
>> list. This would be the prime counting function pi(n). You can then
>> count the primes between a and b in constant time with pi[b] - pi[a].
>
> I did wonder whether, after creating the sieve, I should simply go
> through the list and replace the True values with a count.  This would
> certainly speed up the prime count function, which is where the issue
> arises.  I will try this and see what sort of performance trade-offs
> this involves.
>

By doing that replacement, you'd increase memory usage manyfold (maybe 
3:1, I don't know).  As long as you're only using bools in the list, you 
only have the list overhead to consider, because all the objects 
involved are already cached (True and False exist only once each).  If 
you have integers, you'll need a new object for each nonzero count.



-- 
DaveA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

List Count Blind Anagram <blindanagram@nowhere.org> - 2013-04-22 12:58 +0100
  Re: List Count Dave Angel <davea@davea.name> - 2013-04-22 08:51 -0400
    Re: List Count Blind Anagram <blindanagram@nowhere.org> - 2013-04-22 14:03 +0100
    Re: List Count Blind Anagram <blindanagram@nowhere.org> - 2013-04-22 14:03 +0100
  Re: List Count Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-04-22 13:13 +0000
    Re: List Count Skip Montanaro <skip@pobox.com> - 2013-04-22 08:57 -0500
    Re: List Count Blind Anagram <blindanagram@nowhere.org> - 2013-04-22 15:15 +0100
      Re: List Count Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-04-22 16:14 +0100
        Re: List Count Blind Anagram <blindanagram@nowhere.org> - 2013-04-22 16:50 +0100
          Re: List Count Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-04-22 17:06 +0100
            Re: List Count Blind Anagram <blindanagram@nowhere.org> - 2013-04-22 17:38 +0100
              Re: List Count Skip Montanaro <skip@pobox.com> - 2013-04-22 12:48 -0500
                Re: List Count Blind Anagram <blindanagram@nowhere.org> - 2013-04-22 20:22 +0100
              Re: List Count Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-04-22 21:18 +0100
                Re: List Count Blind Anagram <blindanagram@nowhere.org> - 2013-04-22 22:25 +0100
                Re: List Count Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-04-23 00:06 +0100
                Re: List Count Blind Anagram <blindanagram@nowhere.org> - 2013-04-23 07:45 +0100
                Re: List Count Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-04-22 23:28 +0000
                Re: List Count Blind Anagram <blindanagram@nowhere.org> - 2013-04-23 08:00 +0100
              Re: List Count Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-04-22 22:03 +0100
                Re: List Count Blind Anagram <blindanagram@nowhere.org> - 2013-04-22 22:32 +0100
                Re: List Count Dave Angel <davea@davea.name> - 2013-04-22 21:47 -0400
                Re: List Count Blind Anagram <blindanagram@nowhere.org> - 2013-04-23 08:02 +0100
            Re: List Count Blind Anagram <blindanagram@nowhere.org> - 2013-04-22 17:38 +0100
        Re: List Count Blind Anagram <blindanagram@nowhere.org> - 2013-04-22 16:50 +0100
      Re: List Count Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-04-22 23:01 +0000
        Re: List Count Blind Anagram <blindanagram@nowhere.org> - 2013-04-23 08:05 +0100
          Re: List Count Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-04-23 12:08 +0100
            Re: List Count Blind Anagram <blindanagram@nowhere.org> - 2013-04-23 12:45 +0100
              Re: List Count Terry Jan Reedy <tjreedy@udel.edu> - 2013-04-23 15:01 -0400
          Re: List Count Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-04-23 14:49 +0000
            Re: List Count Blind Anagram <blindanagram@nowhere.org> - 2013-04-23 17:57 +0100
              Re: List Count Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-04-23 18:45 +0100
                Re: List Count Blind Anagram <blindanagram@nowhere.org> - 2013-04-23 19:30 +0100
                Re: List Count Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-04-23 20:16 +0100
              Re: List Count Terry Jan Reedy <tjreedy@udel.edu> - 2013-04-23 16:00 -0400
                Re: List Count Blind Anagram <blindanagram@nowhere.org> - 2013-04-23 21:41 +0100
              Re: List Count Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-04-23 21:38 +0100
              Re: List Count Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-04-24 01:59 +0000
                Re: List Count Blind Anagram <blindanagram@nowhere.org> - 2013-04-24 10:01 +0100
  Re: List Count Peter Otten <__peter__@web.de> - 2013-04-22 15:22 +0200
  Re: List Count 88888 Dihedral <dihedral88888@googlemail.com> - 2013-04-22 06:36 -0700

csiph-web