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


Groups > comp.lang.python > #82273

Re: Nested loops is strangely slow, totally at a loss.

From Peter Otten <__peter__@web.de>
Subject Re: Nested loops is strangely slow, totally at a loss.
Date 2014-12-10 09:21 +0100
Organization None
References <20141210052025.GA84184@FuzzyHorror> <CAPTjJmrM_nrGhPn2Le7b0iDOL5V035SOE3kVg98_89dVjueynA@mail.gmail.com> <CALwzid=vEcxcyxSN+8jXgC0VNKFBKNz5vZF+MrD7maGh0ECGAg@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.16803.1418199721.18130.python-list@python.org> (permalink)

Show all headers | View raw


Ian Kelly wrote:

> On Tue, Dec 9, 2014 at 11:30 PM, Chris Angelico <rosuav@gmail.com> wrote:
>> Are you sure it isn't? Your 'space' is an iterable cubic
>> cross-product. Your first loop checks (0,0,0) which is the first
>> element returned, and is thus fast... but it also *consumes* that
>> first element. The next time you test it, the entire space gets
>> consumed, looking for another (0,0,0), which won't exist. That means
>> iterating over 580**3 == 195112000 (two hundred million) tuples, and
>> that *is* going to be slow.
> 
> Huh, I wasn't even aware that membership tests worked on iterables with no
> __contains__ method. Seems odd to me that 'x in y' should be supported but
> not 'len(y)'.

To me

def contains(iterable, value):
    for item in iterable:
        if item == value:
            return True
    return False

seems to be a perfectly natural default behaviour. I don't see why

>>> 42 in itertools.count()
True

should require that len(count()) must succeed.

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


Thread

Re: Nested loops is strangely slow, totally at a loss. Peter Otten <__peter__@web.de> - 2014-12-10 09:21 +0100

csiph-web