[Pyrex] Speeding up custom string lowercasing with Pyrex

Lamy Jean-Baptiste jibalamy at free.fr
Wed Oct 31 14:28:29 CET 2007


> Thus, I got speedup of 2 over pure Python.  I wonder whether the
> rest -- 8 times slowdown w.r.t. no-lowercasing run -- is the
> consequence of creating a new x object?!  Or perhaps I could optimize
> the Pyrex version -- e.g., assign source s to target 'to', and just
> walk over chars in index positions?  I also saw in the list
> references to direct pointer arithmetics -- does it make sense here
> to write a C function, and how would I integrate it with Pyrex or
> directly?

I think there's still many possible optimization in the Python function (and thus in the Pyrex one, since they are very similar). In Python, creating objects (including strings) has a high cost. In particular, calling "+" (or "+=") require to create many intermediary strings. You can avoid that by putting all the lowercased characters in a list, and then joining them. You may also look at CStringIO.

In the Pyrex version, you can get some extra optimization by defining the variable, e.g. adding "cdef int n".
You declared "s" as "char*", however, when you call "for c in s", you implicitely transform the char* back into a Python string. Using ONLY char* (and thus handling the string stuff like ord and so on, using ONLY C functions) should speed it up a lot.

Cheer,
Jean-Baptiste Lamy



More information about the Pyrex mailing list