MATLAB Answers

1

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

Asked by Prabhakar on 18 Jan 2011
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.

  0 Comments

Sign in to comment.

3 Answers

Answer by Walter Roberson
on 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.

  0 Comments

Sign in to comment.


Answer by Daniel Shub
on 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.

  0 Comments

Sign in to comment.


Answer by Andreas Goser on 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 Comments

Andreas, a function either _does_ or _does not_ take exactly the same time to execute each time. If you are not using a Real Time Operating System, then you have indefinite waits due to matters outside your control, and timer objects do not solve that problem.
For example, on my desktop at work, it is not uncommon for me to have a long-running symbolic computation on a product from another company. That other product has the habit of running smoothly for a while and then encountering a point at which a different internal routine is invoked with that routine using a lot of memory inside a compiled routine that doesn't check interrupts (for performance). If the program chews through my desktop's physical memory, then it starts swapping to disk -- and at that point, the wait times for even simple key presses become unbounded (and even infinite, as it can lock up the CPU completely). You need an operating system specifically designed to handle this kind of situation in order to have any chance of getting an upper bound on the execution time of a routine. MS Windows for the desktop cannot do it. Most Linux versions cannot do it. SGI's Altix extensions to Linux _might_ be able to handle the situation, if the system was configured to expect it and a wrapper program was used to set Matlab in to real-time mode.
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.
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.)

Sign in to comment.