[Numpy-discussion] seeking advice on a fast string->array conversion
Darren Dale
dsdale24 at gmail.com
Tue Nov 16 10:31:31 EST 2010
On Tue, Nov 16, 2010 at 9:55 AM, Pauli Virtanen <pav at iki.fi> wrote:
> Tue, 16 Nov 2010 09:41:04 -0500, Darren Dale wrote:
> [clip]
>> That loop takes 0.33 seconds to execute, which is a good start. I need
>> some help converting this example to return an actual numpy array. Could
>> anyone please offer a suggestion?
>
> Easiest way is probably to use ndarray buffers and resize them when
> needed. For example:
>
> https://github.com/pv/scipy-work/blob/enh/interpnd-smooth/scipy/spatial/qhull.pyx#L980
Thank you Pauli. That makes it *incredibly* simple:
import time
cimport numpy as np
import numpy as np
cdef extern from 'stdlib.h':
double atof(char*)
def test():
py_string = '100'
cdef char* c_string = py_string
cdef int i, j
cdef double val
i = 0
j = 2048*1200
cdef np.ndarray[np.float64_t, ndim=1] ret
ret_arr = np.empty((2048*1200,), dtype=np.float64)
ret = ret_arr
d = time.time()
while i<j:
c_string = py_string
ret[i] = atof(c_string)
i += 1
ret_arr.shape = (1200, 2048)
print ret_arr, ret_arr.shape, time.time()-d
The loop now takes only 0.11 seconds to execute. Thanks again.
More information about the NumPy-Discussion
mailing list