Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: 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; 'python.': 0.02; 'syntax': 0.04; 'argument': 0.05; 'cpython': 0.05; 'indexing': 0.07; 'arrays': 0.09; 'subject:How': 0.10; 'cc:addr:python-list': 0.11; 'python': 0.11; 'suggest': 0.14; 'array.': 0.16; 'benjamin': 0.16; 'dictionaries': 0.16; 'looping': 0.16; 'numpy': 0.16; 'subject:make': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'numerical': 0.19; 'solution.': 0.20; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; '>': 0.26; 'second': 0.26; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'array': 0.29; "doesn't": 0.30; 'nature': 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'getting': 0.31; '+0100,': 0.31; 'ordinary': 0.31; 'class': 0.32; 'lists': 0.32; 'stuff': 0.32; 'run': 0.32; 'fri,': 0.33; 'comment': 0.34; 'sense': 0.34; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'version': 0.36; 'really': 0.36; 'thanks': 0.36; 'seconds': 0.37; 'e.g.': 0.38; 'lists.': 0.38; 'fact': 0.38; 'expect': 0.39; 'even': 0.60; 'most': 0.60; 'range': 0.61; "you're": 0.61; 'making': 0.63; 'kind': 0.63; 'real': 0.63; 'skip:n 10': 0.64; 'more': 0.64; 'citizens': 0.65; 'jul': 0.74; 'subject:this': 0.83; 'faster.': 0.84; 'improvement,': 0.84; 'oscar': 0.84; 'choice.': 0.93; '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:date:message-id:subject:from:to :cc:content-type; bh=2BEzVvREvJ5tiLg+t28WlZnW5aOxMVrbpMqnWwG1f/w=; b=EcbE5YPHEy7LR98jAn9ImGeDp4VKuwYjG/HDfE0ZWUz2sEkeV7Ra8BYr+e+h75VTUS YZ/q+SubZthRoFq7OW1q11V34rD97fAqw62kntrCqueviVni4XO8YZL3JdSP0PYhht5u ydLv+iatICU5qu6YF2oUatcWTJa50sogA3BTtFxHdJ08+ZwqqLKhj5bYI/sy/XIjBfM+ y3S63Us2eUYIJ9f3Ln2bcN3Lsw6dKlqH6MGHycKTfy4Vgzy2a5QxSKuaUPMYJoe2AsCg MfzabWrrufY5de20NoiqJFu1MzePrBGXid39gHQlz4/SJEfmhlDW2ma33QbE8Zw1XQIy 8YsA== MIME-Version: 1.0 X-Received: by 10.224.123.68 with SMTP id o4mr8860151qar.106.1373028298068; Fri, 05 Jul 2013 05:44:58 -0700 (PDT) In-Reply-To: References: Date: Fri, 5 Jul 2013 13:44:57 +0100 Subject: Re: How to make this faster From: =?ISO-8859-1?Q?F=E1bio_Santos?= To: Helmut Jarausch Content-Type: multipart/alternative; boundary=047d7bd6bc92f91faf04e0c30fda 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 71 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1373028307 news.xs4all.nl 15929 [2001:888:2000:d::a6]:36469 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:49980 --047d7bd6bc92f91faf04e0c30fda Content-Type: text/plain; charset=ISO-8859-1 On 5 Jul 2013 11:58, "Helmut Jarausch" wrote: > > On Fri, 05 Jul 2013 11:13:33 +0100, Oscar Benjamin wrote: > > > 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. > > > > Thanks Oscar, that was a big improvement, indeed. > Using lists of lists instead of numpy arrays made the code more than > twice as fast (13 seconds down to 6 seconds) > > Since I don't do any numerical stuff with the arrays, Numpy doesn't seem to be > a good choice. I think this is an argument to add real arrays to Python. > > 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. May I suggest you avoid range and use enumerate(the_array) instead? It might be faster. --047d7bd6bc92f91faf04e0c30fda Content-Type: text/html; charset=ISO-8859-1


On 5 Jul 2013 11:58, "Helmut Jarausch" <jarausch@igpm.rwth-aachen.de> wrote:
>
> On Fri, 05 Jul 2013 11:13:33 +0100, Oscar Benjamin wrote:
>
> > 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.
> >
>
> Thanks Oscar, that was a big improvement, indeed.
> Using lists of lists instead of numpy arrays made the code more than
> twice as fast (13 seconds down to 6 seconds)
>
> Since I don't do any numerical stuff with the arrays, Numpy doesn't seem to be
> a good choice. I think this is an argument to add real arrays to Python.
>
> 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.

May I suggest you avoid range and use enumerate(the_array) instead? It might be faster.

--047d7bd6bc92f91faf04e0c30fda--