Does "persistent variable" help speed?
17 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposte (1)
Yair Altman
il 11 Nov 2015
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 Commenti
Yair Altman
il 11 Nov 2015
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.
Vedere anche
Categorie
Scopri di più su Scope Variables and Generate Names in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!