[Pyrex] Speeding up custom string lowercasing with Pyrex

Bob Van Zant bob at norcalttora.com
Wed Oct 31 14:48:09 CET 2007


On 10/31/07 6:32 PM, "Alexy Khrabrov" <deliverable at gmail.com> wrote:
> 
> def rulower(char *s):
> to = ""
> for c in s:
> n = ord(c)
> # Russian Caps: 0xc0-0xdf
> if (0xC0 <= n and n < 0xE0) or (n == 0xA8) or (0x41 <= n and n <
> 0x5B):
> n = n + 32
> c = chr(n)
> #else:
> # pass
> to = to + c
> return to

The "to = to + c" line strikes me as not being efficient. Python creates a
new copy of "to" every time it appends "c" to the end. One common,
pure-python fix is:

to = []
for ...
   ...
   to.append( c )
return "".join(to)

You don't even need to use Pyrex to get the speedup here.

You could use a regular char * and that would let you modify characters in
place, however, I imagine that the speedup you'll get from the "".join()
will be sufficient.

-Bob







More information about the Pyrex mailing list