Sturla Molden wrote:
> Dag Sverre Seljebotn wrote:
>   
>> Well, there's no way a float** will fit within the NumPy memory model, so
>> the memory must be copied somehow. Using a memcpy per row will likely be
>> faster but is fundamentally the same thing.
>>     
> There is, if a contiguous block is allocated instead of individual rows.
>
> double **array = malloc(nrows * sizeof(double*));
> double *array_buffer = malloc(nrows * ncols * sizeof(double));
> for (int i=0; i<nrows; i++)
>     array[i] = array_buffer + i * ncols;
>   
Actually, one could improve this even further, allocating everything 
into one block:

void *array_buffer = malloc(nrows * ncols * sizeof(double) + nrows * 
sizeof(double*));

double **array = (double **)((char *)array_buffer + nrows * ncols * 
sizeof(double));
for (int i=0; i<nrows; i++)
    array[i] = (double *)array_buffer + i * ncols;


S.M.


_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to