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


Groups > comp.lang.python > #82263

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

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'element': 0.07; '*is*': 0.09; 'logic': 0.09; 'slow.': 0.09; 'cc:addr:python-list': 0.11; '"in"': 0.16; 'element.': 0.16; 'exist.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'iterable': 0.16; 'iterating': 0.16; 'iteration.': 0.16; 'itertools': 0.16; 'returned,': 0.16; 'subject:slow': 0.16; 'tuples,': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'import': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'pass': 0.26; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'thus': 0.29; 'dec': 0.30; 'message-id:@mail.gmail.com': 0.30; 'concern': 0.31; 'another': 0.32; 'problem': 0.35; 'test': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'next': 0.36; 'example,': 0.37; 'represent': 0.38; 'checks': 0.38; 'pm,': 0.38; 'sure': 0.39; 'skip:p 20': 0.39; 'space': 0.40; 'skip:x 10': 0.40; 'entire': 0.61; 'first': 0.61; 'to:none': 0.92; 'hundred': 0.95
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=IUPt7ch5PETXbh76IU/0/ATYhbkCimWJt39fiYhyYs0=; b=glHokEmH58GZ9o3UdYOen+kEgLsYBJia0bXihAo/WjDQMxlubOisK9vRrWv1RDQzsm IxDFtwakoVRHHXrWXBCJW+5C/J7j4JoWzaBZ1iLFFiRWO8xI1ANa0f86mVhqWCgxd9qI tNTTLhtYfrgDW3XiUlq5A+4EVNQLgVXKVb6n3tO3zewvgrLmEukqSh+cPzcOrYgIybQS 81mx9M96UYLOruWE3+HWpPQHMQONJfSTXrthB2ZEgEK2KfE97z7e2NUwOSGYP+ps0p08 cgplnHOP/UJzG5IxdX10NTITSKNwd5NZLm9154Sw4ZKM7884yWfmTdQTG7xE569NYG/6 RvyQ==
MIME-Version 1.0
X-Received by 10.42.130.194 with SMTP id w2mr5399330ics.12.1418193005154; Tue, 09 Dec 2014 22:30:05 -0800 (PST)
In-Reply-To <20141210052025.GA84184@FuzzyHorror>
References <20141210052025.GA84184@FuzzyHorror>
Date Wed, 10 Dec 2014 17:30:05 +1100
Subject Re: Nested loops is strangely slow, totally at a loss.
From Chris Angelico <rosuav@gmail.com>
Cc Python <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.16796.1418193013.18130.python-list@python.org> (permalink)
Lines 27
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1418193013 news.xs4all.nl 2922 [2001:888:2000:d::a6]:42047
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:82263

Show key headers only | View raw


On Wed, Dec 10, 2014 at 4:20 PM, Shiyao Ma <i@introo.me> wrote:
> from itertools import product
> space_len = 580
> space = product(xrange(space_len), xrange(space_len), xrange(space_len))
>
> sparse_cloud = product(xrange(1000), xrange(1000), xrange(1000))
> for i, j, k in sparse_cloud:
>     ts = timeit.default_timer()
>     if (i, j, k) in space: pass
>     te = timeit.default_timer()

On Wed, Dec 10, 2014 at 4:23 PM, Shiyao Ma <i@introo.me> wrote:
> One thing to note, the logic of using "in" is not of concern here.
> This is a *contrived* example, the problem is the slowness of the first
> iteration.

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.

Is product() really the right way to represent your space?

ChrisA

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


Thread

Re: Nested loops is strangely slow, totally at a loss. Chris Angelico <rosuav@gmail.com> - 2014-12-10 17:30 +1100

csiph-web