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


Groups > comp.lang.python > #49960

Re: How to make this faster

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!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.026
X-Spam-Evidence '*H*': 0.95; '*S*': 0.00; 'python.': 0.02; 'algorithm': 0.04; 'syntax': 0.04; 'cpython': 0.05; 'indexing': 0.07; 'processing.': 0.07; 'subject:How': 0.10; 'cc:addr:python- list': 0.11; 'python': 0.11; 'def': 0.12; 'array.': 0.16; 'c++.': 0.16; 'looping': 0.16; 'numpy': 0.16; 'optimised': 0.16; 'subject:make': 0.16; 'wrote:': 0.18; 'library': 0.18; 'numerical': 0.19; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'second': 0.26; 'header:In-Reply- To:1': 0.27; 'function': 0.29; 'nature': 0.30; 'message- id:@mail.gmail.com': 0.30; 'code': 0.31; 'getting': 0.31; 'easier': 0.31; '(on': 0.31; '3.2': 0.31; 'coded': 0.31; 'ordinary': 0.31; 'class': 0.32; 'lists': 0.32; 'run': 0.32; 'another': 0.32; 'comment': 0.34; 'sense': 0.34; 'core': 0.34; 'could': 0.34; 'problem': 0.35; 'operations': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'version': 0.36; 'really': 0.36; 'doing': 0.36; 'shows': 0.36; 'hi,': 0.36; 'should': 0.36; 'seconds': 0.37; 'expected': 0.38; 'implement': 0.38; 'problems': 0.38; 'e.g.': 0.38; 'lists.': 0.38; 'fact': 0.38; 'expect': 0.39; 'does': 0.39; 'called': 0.40; 'skip:u 10': 0.60; 'solve': 0.60; 'most': 0.60; 'course': 0.61; 'simple': 0.61; "you're": 0.61; 'first': 0.61; 'times': 0.62; 'making': 0.63; 'kind': 0.63; 'july': 0.63; 'skip:n 10': 0.64; 'more': 0.64; 'citizens': 0.65; 'subject:this': 0.83; '(only)': 0.84; '(probably': 0.84; 'one).': 0.84; 'oscar': 0.84; 'sacrificing': 0.84; '***': 0.95; '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=KsP2nxqjvTLrOHMCkrP66YYrnheNlMcSjRbpSFmF6K4=; b=oEO6TNW0GM76yOSF+z9g/vEywreA44VG2Oq0OzoRK4L6NxEQXQBghjdQIrqPys8sk6 W1CJwVrDHV9MQKEgGzroCtIdpbVI5HMnRHYZL5ivYhUKbLyCSbkBSzrueDeS3UafZRMc RXOzVNVnv6yWBVq+HPU2LJ1UtgybMHPNaajV5gqIrvSw4OghhsBZniaFi2X2ld0UE3Wa iRXLBBm+TMhDBRkvbtwDTQ6nCbMrR/kDSthEpN2zk+OjhPKXsvXn9lKp+Rl0BPMCHlcC JHF68CzmkBtaXF5e6X4HpGSMfeoFyPX/mIz9QoJ8VmNRjufDVfChTXQ8fM11mJBqhGEd 51ww==
X-Received by 10.58.207.135 with SMTP id lw7mr6123870vec.92.1373019233374; Fri, 05 Jul 2013 03:13:53 -0700 (PDT)
MIME-Version 1.0
In-Reply-To <b3ne2rFojnkU1@mid.dfncis.de>
References <b3ne2rFojnkU1@mid.dfncis.de>
From Oscar Benjamin <oscar.j.benjamin@gmail.com>
Date Fri, 5 Jul 2013 11:13:33 +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.4288.1373019242.3114.python-list@python.org> (permalink)
Lines 57
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1373019242 news.xs4all.nl 15999 [2001:888:2000:d::a6]:50965
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:49960

Show key headers only | View raw


On 5 July 2013 09:22, Helmut Jarausch <jarausch@igpm.rwth-aachen.de> wrote:
> Hi,
>
> I have coded a simple algorithm to solve a Sudoku (probably not the first one).
> Unfortunately, it takes 13 seconds for a difficult problem which is more than 75 times slower
> than the same algorithm coded in C++.
> Is this to be expected or could I have made my Python version faster *** without *** sacrificing readability.

It is to be expected that this kind of processing is faster in C than
in straight Python. Where Python can win in these kind of problems is
if it makes it easier to implement a better algorithm but if your code
is just a transliteration from C to Python you should expect it to run
slower. Another way that Python can win is if you can express your
problem in terms of optimised operations from e.g. the numpy library
but you're not doing that here.

Of course that does depend on your Python implementation so you could
try e.g. using PyPy/nuitka/cython etc. to speed up the core
processing.

> Profiling shows that the function find_good_cell is called (only) 45267 times and this take 12.9 seconds
> CPU time (on a 3.2 GHz machine)
[snip]
>
> def find_good_cell() :
>   Best= None
>   minPoss= 10
>   for r in range(9) :
>     for c in range(9) :
>       if  Grid[r,c] > 0 : continue
>       Sq_No= (r//3)*3+c//3
>       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 ( Possibilities < minPoss ) :
>         minPoss= Possibilities
>         Best= (r,c)
>
>   if minPoss == 0 : Best=(-1,-1)
>   return Best

My one comment is that you're not really making the most out of numpy
arrays. Numpy's ndarrays are efficient when each line of Python code
is triggering a large number of numerical computations performed over
the array. Because of their N-dimensional nature and the fact that
they are in some sense second class citizens in CPython they are often
not as good as lists for this kind of looping and indexing.

I would actually expect this program to run faster with ordinary
Python lists and lists of lists. It means that you need to change e.g.
Grid[r, c] to Grid[r][c] but really I think that the indexing syntax
is all you're getting out of numpy here.


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