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


Groups > comp.lang.python > #49989

Re: How to make this faster

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder7.xlned.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <oscar.j.benjamin@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.009
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'algorithm': 0.04; 'anyway.': 0.05; 'column': 0.07; 'indices': 0.07; 'builtin': 0.09; 'grid': 0.09; 'subject:How': 0.10; 'cc:addr:python-list': 0.11; 'python': 0.11; 'benjamin': 0.16; 'dict': 0.16; 'dictionaries': 0.16; 'exists)': 0.16; 'innermost': 0.16; 'iterating': 0.16; 'looping': 0.16; 'numpy': 0.16; 'subject:make': 0.16; 'thanks,': 0.17; 'wrote:': 0.18; 'bit': 0.19; 'meant': 0.20; 'solution.': 0.20; 'seems': 0.21; '>>>': 0.22; 'cc:addr:python.org': 0.22; 'error': 0.23; 'switched': 0.24; 'looks': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'certain': 0.27; 'header:In-Reply- To:1': 0.27; 'tried': 0.27; 'array': 0.29; 'message- id:@mail.gmail.com': 0.30; 'gives': 0.31; 'program,': 0.31; 'easier': 0.31; '+0100,': 0.31; 'comparison': 0.31; 'keys': 0.31; 'lists': 0.32; 'languages': 0.32; 'stuff': 0.32; 'fri,': 0.33; 'could': 0.34; 'something': 0.35; 'operations': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'version': 0.36; 'entry': 0.36; 'seconds': 0.37; 'list': 0.37; 'implement': 0.38; 'e.g.': 0.38; 'rather': 0.38; 'little': 0.38; 'expect': 0.39; 'even': 0.60; 'gone': 0.61; 'range': 0.61; "you're": 0.61; 'first': 0.61; 'kind': 0.63; 'july': 0.63; 'improvement.': 0.68; 'jul': 0.74; 'obvious': 0.74; 'subject:this': 0.83; 'trial': 0.83; 'hood': 0.84; 'occupied': 0.84; 'optimisation': 0.84; 'oscar': 0.84; 'presumably': 0.84; '2013': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=0c5EfhuQG2qdNpI+v1VrVd4WjeBq5yhSHaQX92xI+vo=; b=yHZQmaapaNejYRgY6iIl0Prus6wCJuP3i1p0ydN+//KEAOi0IEmbn5T9D+tv1At9bK uJ8+ZR/YQeMkdEjdPtjiqZMtNXoqIzBBXP85nxQsuhD5PCP/VdsCs7OS6ZacNcpreZy8 WcpZQxzcNjkBx2Mb+zDWrb84Njo7rYPQvL5pSAReYFllAk00cXj8iPdPqPyRk98fCY16 yd8i3FNIoYcZJUTAtCIbBe2tQrAVpM4ixfacTt73FsF/RWA874Vq8r5V5HdJNUmqmLon 2FuZTERNLyEJL/M7oxNXF//kxorZT/60stung4c/kkafOP4VZj/Rbeg4ljkQNXelTwkf 2s7g==
X-Received by 10.220.6.135 with SMTP id 7mr6979214vcz.72.1373035545292; Fri, 05 Jul 2013 07:45:45 -0700 (PDT)
MIME-Version 1.0
In-Reply-To <b3o3gkFtc39U1@mid.dfncis.de>
References <b3ne2rFojnkU1@mid.dfncis.de> <b3nmtfFojnkU3@mid.dfncis.de> <b3o3gkFtc39U1@mid.dfncis.de>
From Oscar Benjamin <oscar.j.benjamin@gmail.com>
Date Fri, 5 Jul 2013 15:45:25 +0100
Subject Re: How to make this faster
To Helmut Jarausch <jarausch@igpm.rwth-aachen.de>
Content-Type text/plain; charset=ISO-8859-1
Cc python-list@python.org
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 <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.4299.1373035554.3114.python-list@python.org> (permalink)
Lines 61
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1373035554 news.xs4all.nl 15975 [2001:888:2000:d::a6]:40803
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:49989

Show key headers only | View raw


On 5 July 2013 15:28, Helmut Jarausch <jarausch@igpm.rwth-aachen.de> wrote:
> On Fri, 05 Jul 2013 14:41:23 +0100, Oscar Benjamin wrote:
>
>> On 5 July 2013 11:53, Helmut Jarausch <jarausch@igpm.rwth-aachen.de> wrote:
>>> I even tried to use dictionaries instead of Numpy arrays. This version is a bit
>>> slower then the lists of lists version (7.2 seconds instead of 6 second) but still
>>> much faster than the Numpy array solution.
>>
>> When you switched to dictionaries did you take advantage of the
>> sparseness by iterating over dictionary keys instead of indices? This
>> is the kind of thing that I meant when I said that in Python it's
>> often easier to implement a better algorithm than in C. What I mean is
>> that if Grid is a dict so that Grid[(r, c)] is the entry at row r and
>> column c (if it exists) then you can change a loop like:
>>
>>     for r in range(9):
>>         for c in range(9):
>>             if Grid[r, c] > 0: continue
>>             # do stuff
>>
>> so that it looks like:
>>
>>     for r, c in Grid:
>>         # do stuff
>>
>> If the grid is sparsely occupied then this could be a significant improvement.
>>
> This gives a big speedup. Now, the time is gone down to 1.73 seconds in comparison to
> original 13 seconds or the 7 seconds for the first version above.

Presumably then you're now down to the innermost loop as a bottle-neck:

      Possibilities= 0
      for d in range(1,10) :
        if Row_Digits[r,d] or Col_Digits[c,d] or Sqr_Digits[Sq_No,d] : continue
        Possibilities+= 1

If you make it so that e.g. Row_Digits[r] is a set of indices rather
than a list of bools then you can do this with something like

    Possibilities = len(Row_Digits[r] | Col_Digits[c] | Sqr_Digits[Sq_No])

or perhaps

    Possibilities = len(set.union(Row_Digits[r], Col_Digits[c],
Sqr_Digits[Sq_No]))

which I would expect to be a little faster than looping over range
since the loop is then performed under the hood by the builtin
set-type.

> Many thanks,
> it seems hard to optimize a Python program,

It just takes practice. It's a little less obvious in Python than in
low-level languages where the bottlenecks will be and which operations
are faster/slower but optimisation always involves a certain amount of
trial and error anyway.


Oscar

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


Thread

How to make this faster Helmut Jarausch <jarausch@igpm.rwth-aachen.de> - 2013-07-05 08:22 +0000
  Re: How to make this faster Fábio Santos <fabiosantosart@gmail.com> - 2013-07-05 10:38 +0100
  Re: How to make this faster Helmut Jarausch <jarausch@igpm.rwth-aachen.de> - 2013-07-05 10:07 +0000
  Re: How to make this faster Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-07-05 11:13 +0100
  Re: How to make this faster Helmut Jarausch <jarausch@igpm.rwth-aachen.de> - 2013-07-05 10:53 +0000
    Re: How to make this faster Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-05 12:02 +0000
    Re: How to make this faster Fábio Santos <fabiosantosart@gmail.com> - 2013-07-05 13:44 +0100
    Re: How to make this faster Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-07-05 14:41 +0100
    Re: How to make this faster Helmut Jarausch <jarausch@igpm.rwth-aachen.de> - 2013-07-05 14:28 +0000
      Re: How to make this faster Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-07-05 15:45 +0100
    Re: How to make this faster Helmut Jarausch <jarausch@igpm.rwth-aachen.de> - 2013-07-05 14:48 +0000
      Re: How to make this faster Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-07-05 16:26 +0100
    Re: How to make this faster Helmut Jarausch <jarausch@igpm.rwth-aachen.de> - 2013-07-05 14:54 +0000
      Re: How to make this faster Fábio Santos <fabiosantosart@gmail.com> - 2013-07-05 16:18 +0100
      Re: How to make this faster Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-05 16:24 +0000
    Re: How to make this faster Helmut Jarausch <jarausch@igpm.rwth-aachen.de> - 2013-07-05 15:17 +0000
      Re: How to make this faster Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-07-05 16:38 +0100
      Re: How to make this faster MRAB <python@mrabarnett.plus.com> - 2013-07-05 17:25 +0100
      Re: How to make this faster Joshua Landau <joshua.landau.ws@gmail.com> - 2013-07-06 02:45 +0100
    Re: How to make this faster Helmut Jarausch <jarausch@igpm.rwth-aachen.de> - 2013-07-05 15:47 +0000
      Re: How to make this faster Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-05 16:52 +0000
    Re: How to make this faster Helmut Jarausch <jarausch@igpm.rwth-aachen.de> - 2013-07-05 16:07 +0000
      Re: How to make this faster Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-05 16:50 +0000
        Re: How to make this faster Helmut Jarausch <jarausch@igpm.rwth-aachen.de> - 2013-07-05 18:39 +0000
          Re: How to make this faster Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-06 03:05 +0000
          Re: How to make this faster Helmut Jarausch <jarausch@igpm.rwth-aachen.de> - 2013-07-06 07:25 +0000
    Re: How to make this faster Helmut Jarausch <jarausch@igpm.rwth-aachen.de> - 2013-07-05 18:42 +0000

csiph-web