I have a Matlab function (my code) that takes about 1.5ms to run most of the time, but every once in a while spikes up - 10ms, 15ms, 20ms, or even higher. I would like to figure out why.
I may have to do this with other functions in future - so my question is not about this specific piece of code, but rather about how to debug this type of a problem in Matlab.
1) tic/toc - Above timings were done using tic/toc. Natural question is - since I am measuring elapsed time, are my outlier purely due to things like thread switches, etc. etc.? If so, would setting processor affinity of the Matlab process help?
2) cputime - I tried using cputime - for a lot of my calls, the cputime returns zero even when tic/toc returns a non-zero value. Has anyone experienced this?
3) Profiler - Pretty useless in this case I think - it reports average times. Well, on average, the function performs just fine. I am trying to track down the outliers, and I don't quite see how the Profiler can be helpful here.
Thanks in advance! matal
They link to other resources on timing.
BTW: Which OS do you use?
First time ever(?) I try cputime. The resolution is poor (0.0156s on Windows 7, R2012a 64bit). That's why you see ZERO.
>> t = cputime; surf(peaks(40)); e = cputime-t e = 0.2028 >> t = cputime; surf(peaks(40)); e = cputime-t e = 0.0624 >> t = cputime; surf(peaks(40)); e = cputime-t e = 0.0156 >> t = cputime; surf(peaks(40)); e = cputime-t e = 0.0156 >> t = cputime; surf(peaks(40)); e = cputime-t e = 0.0312 >> t = cputime; surf(peaks(40)); e = cputime-t e = 0.0156 >> t = cputime; surf(peaks(40)); e = cputime-t e = 0
The Profiler DOES provide detailed information about specific call invocations, but you need to dig a bit in its undocumented raw data. This is explained here: http://undocumentedmatlab.com/blog/undocumented-profiler-options-part-3/
Naturally, the raw data begs for someone to write a utility that presents the info in a more human-readable manner. I would have done it myself if I had any time - maybe someone else will pick up the glove (Jan? Per? anyone?)
Measuring the timing can be influenced by other applications also. E.g. the garbage collection of the operating system's memory manager. Or the WLAN adapter checks the connection. I do not think that the source of the delays can be reliably determined in every case. So a strategy for measuring timings is to exclude the outliers from the statistics.
Play games and win prizes!Learn more