How could I accelerate m-file code?

1 次查看(过去 30 天)
Douglas Freire
Douglas Freire 2017-2-7
回答: Jan 2017-2-7
I'm trying to implement model predictive control to DC motor. MPC code was implemented in scripts, without toolboxes, and DC motor model was identified by nlarx method. I need that control loop run at 0.1 seconds, but it is running average at 0.5 seconds. I'm using preallocated variables and parfor functions to accelerate the code. I could use discrete equation instead sim function, but I dont know how to get explicit equation from idnlarx model.
How could I accelerate even more the m-file code?

回答(1 个)

Jan
Jan 2017-2-7
Do not use
warning off; clear all; close all; clc;
because clear all removes all loaded function from the RAM and reloading the files from the slow hard disk wastes a lot of time.
Calling WMIC through system is time consuming also and as far as I cen see, this is applied to the same Matlab session all the time.
Matlab's JIT acceleration works only for lines, which contain one command. At least this was the situation in the time, when this feature was documented. Although modern Matlab versions might not suffer from this limitation, having one command per line only improves the readability.
If you are sure, that all variables are pre-allocated properly, then I do not have more general methods. Use the profiler to find the bottleneck of the code, because it is not worth to optimize a line, which takes 0.1% of the processing time only: you can reduce the runtime by 0.1% only, if you can accelerate this line by a factor of even Infinity.

类别

Help CenterFile Exchange 中查找有关 Simulation 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by