Does "persistent variable" help speed?
17 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (1)
Yair Altman
el 11 de Nov. de 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 comentarios
Yair Altman
el 11 de Nov. de 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.
Ver también
Categorías
Más información sobre Scope Variables and Generate Names en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!