Global workspace usage for efficiency?
이전 댓글 표시
Are there cases when using global variables is the most efficient implementation?
I hate using the global workspace, but I end up doing it all the time. There is only one reason that I have had for doing this: performance/efficiency.
For concrete examples, I have two heavily optimized submissions on the FEX: a Particle simulator and real time motion from video
I am well aware of how JIT acceleration works, and how Matlabs in-place operations works. The functions setappdata/getappdata seem to be good candidates for replacing the use of global workspace, however consider a situation where you modify the data in the calling function. If, as are the cases in the two FEX submissions described above, I call functions often and and share large amounts of data, isnt the global workspace more efficient than other solutions?
The answer to this question in Matlab before 2007 I would say definetly yes: the global workspace is the fast, but ugly way. I dont feel to old to learn new tricks, so please enlighten me here, and the next code i make will be free of globals.
Note that milliseconds count.
답변 (2개)
Walter Roberson
2016년 1월 15일
2 개 추천
global is pretty much the slowest access; see http://www.mathworks.com/matlabcentral/answers/99537-which-type-of-function-call-provides-better-performance-in-matlab
However I suspect a lot of the time there is in locating the data. If you are in a mex routine you would grab the appropriate pointer and maybe data pointer as well and there would be no further cost penalty beyond copy-on-write semantics.
댓글 수: 6
Stefan Karlsson
2016년 1월 15일
Stefan Karlsson
2016년 1월 15일
Walter Roberson
2016년 1월 15일
shared variables via nested functions would be the most efficient for that case.
setappdata() has the benefit of being able to pull back a particular substructure, and so is more efficient than guidata which has to copy everything.
handle objects can also provide efficient access to data in multiple scopes -- though you do have to be careful to not allow all references to the objects to go out of scope. (Persistence of objects in MATLAB is something I have not read much about.)
Stefan Karlsson
2016년 1월 16일
Walter Roberson
2016년 1월 17일
I think getappdata() of a unique name should be faster than global of the same name, but I would need to test to be sure.
Stefan Karlsson
2016년 1월 18일
Stefan Karlsson
2016년 1월 20일
카테고리
도움말 센터 및 File Exchange에서 Debugging and Analysis에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!