How can I make a MATLAB program to take exactly the same time to run, every time it is run in MATLAB?

3 Ansichten (letzte 30 Tage)
I have a MATLAB program which takes a certain amount of time to run, however I see that the time taken to execute in MATLAB is not the same always and I want know if there are any means by which one can run the .m code every time the same time when run in MATLAB. I am not looking at any improvement in the time taken to execute but want fixed time duration every time it is executed.

Antworten (3)

Walter Roberson
Walter Roberson am 20 Jan. 2011
You can only do this by using the Real Time Workshop running on a target which includes a Real-Time Operating Systems. In any other situation, there are other operations attempting to run at the same time that Matlab is running, making the Matlab run time variable.
Any routine which does not pre-allocate all memory (including that used temporarily) may take an indefinite amount of time to locate available memory. Furthermore, Mathworks does not provide any user-level control over when internal routines such as garbage-collection will be run.
The time taken for any interaction with the operating system or disk I/O is inherently variable with operating systems that are not designed as Real Time Operating Systems (and even then one may have to call special real-time versions of the routines to get real-time response)
MS Windows is not designed as a Real Time Operating System. Some versions of Linux are able to run in real-time modes when specially configured.

Daniel Shub
Daniel Shub am 20 Jan. 2011
The only way to get the code to run the same amount of time every time, is to make every time run in the slowest time. So if usually your code takes 1 second to run, but when your computer does something else (e.g., virus scan or backup program) the code takes 5 minutes, then you need the code to take 5 minutes every time. This is a pretty big decrease in performance.
My suggestion is to run the code a bunch of times saving the execution time in x. Then let T0=mean(X)+n*std(X), with n chosen based on how confident you want to be that the code will not take longer than T0. Then modify your code to get the time when your code starts (clock will work, but there might be better timing solutions) and stick a while loop at the end of the code to wait until T0 has elapsed from the beginning.

Andreas Goser
Andreas Goser am 20 Jan. 2011
I do not support the Real-Time Workshop suggestion, as this product is designed for code generation of Simulink models (although embedded MATLAB code can be used too).
You can leverage Daniel's suggestion by using timer objects:
  3 Kommentare
Daniel Shub
Daniel Shub am 20 Jan. 2011
A function either does or does not take the same time within some +/- epsilon. If you are talking about millisecond timing and allow for a failure once every Y amount of time then clock (or a timer) and MS Windows will probably work.
Walter Roberson
Walter Roberson am 20 Jan. 2011
Not in my experience with MS Windows; I found multi-second latencies to be not uncommon.
The original question was asked in cssm not so long ago, and garbage collection effects were discussed at that time. Unless you use entirely global or persistent variables and are careful to overwrite storage instead of allowing new storage to be allocated, then you are going to have random garbage collections happening even if just due to the allocation and deallocation of the workspace and associated variables for functions (since the question has to do with the timing for a "program", not just a section of code that is certain not to call upon any subroutines.)

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Introduction to Installation and Licensing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by