Matthew Brett skrev:
>>
>> cdef object _kwargs = {'shape': (n,), dtype=dt, buffer=data}
>> cdef object _ndarray = np.ndarray
>>
>> arr = _ndarray(**_kwargs)
>
> That does seem to make a small difference, but the C-API is still
> about 4 times faster, in my hands
You didn't use it right. You have to manually hoist out the loop
invariants. Obviously, declaring two local variables _kwargs and
_ndarray doesn't help if you do it for each array you construct.
Say you want to construct 1000 similar arrays:
cdef object _kwargs = {'shape':(n,),'dtype':dt,'buffer':data}
cdef object _ndarray = np.ndarray
cdef list arrays = [None]*1000
cdef int i
for i in range(1000):
arrays[i] = _ndarray(**_kwargs)
You should thus modify sturlabench.pyx like this:
cdef dict _kwargs = {'shape':(50,),'dtype':cnp.float64,'buffer':None}
cdef object _ndarray = cnp.ndarray
cdef cnp.npy_intp _size = 50
cdef cnp.dtype _dt = cnp.float64
cdef object _data = None
def make_local_1d_array(cnp.npy_intp size, object data, cnp.dtype dt):
global _kwargs, _size, _dt, _data, _ndarray
if (size != _size):
_kwargs['shape'] = (size,)
_size = size
if (dt is not _dt):
_kwargs['dtype'] = dt
_dt = dt
if (data is not _data):
_kwargs['buffer'] = data
_data = data
return _ndarray(**_kwargs)
I'd also like to point out this: Even with the non-optimized "Python" version
(py_make_1d_array), creating 1000 arrays will only take 3.5 ms with your
timings. "Premature optimization is the root of all evil in computer
programming..." (C.A.R. Hoare, according to D. Knuth).
Regards,
Sturla Molden
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev