Is it possible to analysis executed operations of a Matlab code?
11 views (last 30 days)
Show older comments
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.
1 Comment
Adam
on 19 Jan 2017
The profiler gives a breakdown of numbers of calls. I'm not sure if it goes as deep down the tree as basic plus, multiplication operations as they have never been of interest to me, but it certainly gives more information than just time.
Accepted Answer
Jan
on 19 Jan 2017
Edited: Jan
on 27 May 2021
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.
More Answers (1)
Walter Roberson
on 19 Jan 2017
No, that feature has not been available for a decade or so. The high performance routines used could no longer measure flops, and the measurement was getting increasingly meaningless with newer CPUs.
There have been a small number of posts about modeling the energy usage of VHDL or FPGA, but I cannot seem to locate those at the moment.
How low of a processor are you thinking of deploying to? For example, ARM processors (ARM7 and later at least, not sure about earlier) use multistage pipelining, which can make the cost of instructions more difficult to calculate. I also see in an article that there are floating point accelerators and SIMD (Single Instruction Multiple Data) processors for ARM, which again confuse costs -- a calculation that can be put through SIMD can execute faster than linearly in the number of operations.
1 Comment
John D'Errico
on 19 Jan 2017
This is why flops has been gone for a long time. Simple flop counts are not at all valid any more, given the abilities of processors today, and things will get only worse. Sadly, thesis advisors, reviewers, etc., all think that flops counts still make sense. Yes, you can compute theoretical flop counts for simple algorithms, valid or not on your CPU. But asking your computer to do so for you simply won't work anymore.
See Also
Categories
Find more on Array and Matrix Mathematics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!