0

If I have a function with a return value, like string_at(ptr, size) from ctypes, and I dont use the returned string, is the call to this function removed by some kind of optimization? Or, is any performance improvement only because the string that should be returned, is never really copied to any variable/other memory address, and therefore the time to copy has been saved?

I try to understand this, as I want to achieve to collect images from a camera at a specific framerate. But it seems that this string_at(ptr, size) call is slowing down the process. But, when I use the function without writing the return value to a variable, the performance can be achieved.

More information about the camera and the goal:

  • 8 cameras from ximea that work synchronously
  • The images are transferred from the camera to a ximea switch and from there to the PC via a PCI-e cable
  • The images are transferred at the desired framerate (100 fps per camera)
  • The collection is currently working in one thread, as the images should be collected at the "same" time. So in every collection step, an image from every camera should be collected.
  • current achieved framerate is around 40 fps
IAmRoot
  • 1
  • 7
  • You can use `_` to safely ignore the return value. i.e `_,_=someFunc()` will ignore 2 values by returned by function. `_,_,var=somefunc()` will ignore first 2 values and take the third value. – Ahmad Anis Apr 28 '21 at 06:58
  • 2
    You seem to be expecting very C-like behavior - a strong optimizing compiler and pass-by-value semantics. That's not what you get in Python - very little can be optimized away, but almost nothing is ever copied without an explicit copy operation. Particularly, returning an object does not require copying it. – user2357112 Apr 28 '21 at 06:59
  • @user2357112supportsMonica Yeah, I guess that were some of the ideas I got after experiencing the difference of the framerate. So, it seems the copy just doesnt take place as there is no need to if I don't give a variable to do so. – IAmRoot Apr 28 '21 at 10:07
  • @AhmadAnis Thank you, I know that this is the case. My question was more directed at the performance/time differences. Like user2357112supportsMonica is reffering to. – IAmRoot Apr 28 '21 at 10:09
  • The problem that I still have than, is that the framerate is not satisfactory. But it seems like I have no other option than to copy the images with the ```string_at()``` function. As the images are recieved in the desired framerate, the buffer of the images will be overwritten faster than I can get (and copy) them with this function though. But it seems like the problem is not the CPU (16 cores and does not go over 50% usage), nor is it the PCI-e connection, nor any writing problems, as I don't write anything at that stage. So, were can I fix this? RAM? Another faster function? Any tips? – IAmRoot Apr 28 '21 at 10:14
  • `string_at` builds a `bytes` object, which requires a data copy - the `bytes` object needs to own its buffer. (Returning the `bytes` object does not require another copy.) If the amount of data copied is large enough that this is actually a bottleneck, you could speed things up by refactoring your code to eliminate this call *yourself* when it's not needed instead of relying on Python to do it. – user2357112 Apr 29 '21 at 19:30

0 Answers0