[SOLVED] Ctypes arrays, performance of converting pointer to double pointer to python list


Following c-DLL header

    int getDoubleArray(double const** const value);

where the return value is the size of the double array, created in the function.

I’m able to receive the data. But now I would like to convert it to a Python list object (not able to use NumPy here, because of legacy reasons)

After calling the function

    ## val_arr is a POINTER to c_double
    ## x contains the size of the array (count of doubles)
    x = getDoubleArrayFunc (byref(val_arr))

I tried 2 ways to convert it to a Python list

## noob approach
values = []
for i in range(x):
## better (?) approach
values2 = [val_arr[i] for i in range(x)]

I tested it with a list of 20000 entries
the second approach is about 1/3 faster ~1ms vs 1.6 ms

Now the question: is there another way to improve performance (without using numpy or other external libs)?

It should run with Python 3.x (at least 3.7 or newer)


Question was already answered some time ago
Ctypes: fast way to convert a return pointer to an array or Python list

Results after 1000 times converting a list with 20000 entries (in sek)

  • convert duration: 1.6180373
  • convert2 duration: 1.0428218
  • convert3 duration: 0.33130780000000026
  • convert4 (numpy) duration: 0.00794270000000008, here I explicity do a deepcopy into a new memory

Answered By – nobs

Answer Checked By – Dawn Plyler (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *