MATLAB: Is it possible to analysis executed operations of a Matlab code

executed operationsprofile viewer

Hi, Is there a possibility to have matlab do a performance analysis on how many operations were executed? The reason is that the code I am writing should be transfered to a embedded system. However in order to get a good estimation on how fast the program will be executed on the embedded system, I'd like to know how many multiplications, additions, and so on, are executed. The profile viewer only seems to give me the overall execution time, which isn't very helpful because my embedded system most likely won't have a GPU or multiple cores.

Best Answer

You can overload * and + and include a counter:
@double\plus.m:
function C = plus(A, B)
persistent Count
if isempty(Count)
Count = 0;
end
if nargin == 0
C = Count;
end
C = builtin('plus', A, B);
Count = builtin('plus', Count, numel(C)); % [EDITED]
end
Now the additions are counted. This has to be done for each operation you want to examine.
But what do you expect? What's about:
x(1:100) = 1:100;
Of course the indices in double type require some additions to create the both vectors. Then you need some multiplications internally like * sizeof(double) for calculating the required size of memory. Finally it matters, if the the processed blocks match into the processor cache or not. Otherwise the CPU needs some NOPs to wait for the slow RAM access. Perhaps the processor can use this time to process another thread.
Some trigonometric functions might be calculated by the CPU in Matlab, but by a library in the embedded machine. This might even dominate the total processing time.
My conclusion: There is no reliable way to estimate the performance based on any kind of counting "operations". You can guess, that a code, which uses millions of operations, counted by the overloading method above, will take more time than a version, which needs some dozens only. But this is not strong or accurate.