0

Is there a difference between declaring Mats as global variables or local variables, in terms of the amount of time it takes to perform various different convolutions on them? For example, I performed a mean shift filtering on an image, once using a global Mat, and once using a local Mat in main, then passing it as a pointer to a mean shift filtering function. I timed both of them, and they were nearly identical, but not quite the same.

This led me to wonder about the difference between declaring a Mat as a global variable vs. local/pointers. Any help?

Thanks!

Nathan
  • 61
  • 1
  • 1
  • 8

1 Answers1

2

I would say if you are timing the actual convolution then I don't think it will matter.

In general, I think speed depends more on the implementation as opposed to global vs local. If you have to create/delete Mat objects often or need intermediate Mats then if possible pre allocate and use pointers , etc.

Avoiding globals keeps code more modular etc, etc but that is another discussion altogether. Globals are sometimes the "right tool for the job" but typically there is a better solution.

How they are allocated heap vs stack, I would read this :

Global memory management in C++ in stack or heap?

Community
  • 1
  • 1
don_q
  • 415
  • 3
  • 11