Optimizing Strings In Cython
I'm trying to demonstrate to our group the virtues of Cython for enhancing Python performance. I have shown several benchmarks, all that attain speed up by just: Compiling the ex
Solution 1:
I suggest you do your operations on cpython.array.array
s. The best documentation is the C API and the Cython source (see here).
from cpython cimport array
defcfuncA():
cdefstr a
cdefint i,j
for j inrange(1000):
a = ''.join([chr(i) for i inrange(127)])
defcfuncB():
cdef:
str a
array.array[char] arr, template = array.array('c')
int i, j
for j inrange(1000):
arr = array.clone(template, 127, False)
for i inrange(127):
arr[i] = i
a = arr.tostring()
Note that the operations required vary very much on what you do to your strings.
>>>python2 -m timeit -s "import pyximport; pyximport.install(); import cyytn""cyytn.cfuncA()"
100 loops, best of 3: 14.3 msec per loop
>>>python2 -m timeit -s "import pyximport; pyximport.install(); import cyytn""cyytn.cfuncB()"
1000 loops, best of 3: 512 usec per loop
So that's a 30x speed-up in this case.
Also, FWIW, you can take off another fair few µs by replacing arr.tostring()
with arr.data.as_chars[:len(arr)]
and typing a
as bytes
.
Post a Comment for "Optimizing Strings In Cython"