I was wondering if someone could advise me whether there is a better/faster approach to read data from my C program that outputs two lists of size n. I am using ctypes to call the C program.
The loop I show below works by iterating over a number of scans. For each scan two lists are produced (msX, msY). The c_float data is extracted by using list comprehension loop. Is there a better/faster way to convert the c_float_Array obtained from mzP and mzI to msX and msY?
for scan in xrange(nScans):
mzP = (c_float * nPoints)() # pointer to list 1, c_float_Array
mzI = (c_float * nPoints)() # pointer to list 2, c_float_Array
mlLib.readData(filePointer, 1, scan, byref(mzP), byref(mzI))
# The slow part...
msX = [mzP[i] for i in xrange(nPoints)] # list with mzP data
msY = [mzI[i] for i in xrange(nPoints)] # list with mzI data
Let me know if my question is not clear. Thanks for your help in advance.
msX = mzP[:]would be faster than a list comprehension, but why do you need a list instead of directly using the ctypes array? If a ctypes array is missing some method that you need, maybe anarray.arraywill suffice? Starting withmsX = array.array('f', [0]) * nPoints, you can get a ctypes array that shares it viamzP = (c_float * nPoints).from_buffer(msX).