picosdk-python-wrappers icon indicating copy to clipboard operation
picosdk-python-wrappers copied to clipboard

NumPy is optimized for vectorized operations, which makes it

Open fedetony opened this issue 7 months ago • 1 comments

Optimization of ADC acquisition

NumPy is optimized for vectorized operations, which makes it significantly faster than Python loops. To improve the speed of your calculation, try using NumPy directly instead of list comprehensions.

This eliminates the loop altogether, allowing NumPy to perform element-wise operations efficiently. You should see a noticeable speed improvement, especially for large arrays.

We tested these with 250 Mega points and adquisition took 96.072 s using your code

bufferV = [(x * range) / maxADC.value for x in bufferADC]

by changing to:

bufferADC_np = np.array(bufferADC, dtype=np.int64)
bufferV = (bufferADC_np * range) / maxADC.value

took 1.347 s. And if you premultiply the factor

normRange = range/maxADC.value
bufferV =  np.ctypeslib.as_array(bufferADC) * normRange

reduces to 1.022 s

Enjoy :)

Fixes #.

Changes proposed in this pull request:

  • Use numpy to deal with buffers
  • Use vectorial multiplication instead of for looping

fedetony avatar Jun 13 '25 07:06 fedetony