How to make this faster

Fábio Santos fabiosantosart at
Fri Jul 5 14:44:57 CEST 2013

On 5 Jul 2013 11:58, "Helmut Jarausch" <jarausch at> 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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the Python-list mailing list