Does "persistent variable" help speed?
17 次查看(过去 30 天)
显示 更早的评论
Hi, I have a heavy Simulink model with many Matlab function blocks. I deliver C-Code generated from my model to run on a hardware platform. I am wondering if using persistent variables inside Matlab function blocks improves the execution speed or not? Comments appreciated. Thanks!
0 个评论
回答(1 个)
Yair Altman
2015-11-11
Persistent variables can indeed improve execution speed if you use them in a wise manner that utilizes their benefits, i.e. to cache data across separate function invocations, thereby saving the need to reload or recompute the data in each invocation. Of course, you need to ensure that caching the data is ok for your specific algorithm. After all, a fast but incorrect result will always be inferior to a slow but correct one. If you do not use caching, then persistent variables will have no useful performance impact.
In addition to caching, any other improvement to the performance of your Matlab code will naturally improve your model's overall performance.
2 个评论
Yair Altman
2015-11-11
I do not think so. In fact, using temp (not persistent) variables could possibly be optimized internally by Matlab to use registers/stack, which would be faster than using main memory (heap) as is the case for persistent/global vars. Even if this is not optimized, the potential speed savings are probably negligible.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Scope Variables and Generate Names 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!