AlexShkarin / pyLabLib

Python package for device control and experiment automation
http://pylablib.readthedocs.io
GNU General Public License v3.0
125 stars 28 forks source link

IMAQ superfluous copy #60

Open nerdull opened 8 months ago

nerdull commented 8 months ago

Hi Alexey,

First of all, wonderful work on implementing interfaces to so many varieties of instruments!

I have especially benefited a lot from the IMAQ interface for my own project, where I need to interact with an NI PCIe-1433 frame grabber. Although it has been working smoothly, I have one suggestion regarding the performance enhancement. Specifically, I am referring to this line of code in IMAQ.py. The call to numpy.ndarray.copy() looks superfluous to me, as it only increases memory footprint and execution time. For a better performance, one can just return data. At least with my unexhaustive tests, this replacement works well. On the other hand, the gain is sizable, as now I can handle more throughput.

Cheers, Xiangcheng

AlexShkarin commented 8 months ago

Thank you for the kind words!

As far as I remember, this line is required to have predictable behavior in some situations. Up until that point, data numpy array is directly mapped onto the acquisition ring buffer memory. Furthermore, in most cases its value will not get copied in the later code either (unless different frame axes indexing is used). Therefore, as soon as the buffer is cycled through and that frame is overwritten, the perceived values of data will change. Probably, most users do not expect that, but rather assume that the data received from the camera will stay constant.

That being said, I agree that if you intend to process the received frame right away (before it gets overwritten), this copying is not necessary, and it hurts perfromance. In the future, I will likely add an option to forego this copy operation (and similar ones done in other cameras) at the user's risk.