How to find the total memory used during the execution of the code?
76 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
To find the efficiency of my algorithm, I searched this site:
After studying this site, I want to know how much memory is used by my algorithm fpa1 during execution. I have shared fpa1 along with supporting files in the attachment. One can run main to run the algorithm fpa1.
4 commentaires
Walter Roberson
le 12 Fév 2023
Replace
function s=simplebounds(s,Lb,Ub)
% Apply the lower bound
ns_tmp=s;
I=ns_tmp<Lb;
ns_tmp(I)=Lb(I);
% Apply the upper bounds
J=ns_tmp>Ub;
ns_tmp(J)=Ub(J);
% Update this new move
s=ns_tmp;
end
with
function s = simplebounds(s, Lb, Ub)
s = min(max(s, Lb), Ub);
end
Réponses (2)
Dinesh
le 10 Fév 2023
Hi Sadiq Akbar,
The profile function allows you to measure the execution time and memory usage of your code, both for individual functions and for the entire program. To use this function, simply type "profile on" in the command prompt, then run your code, and then type "profile viewer". This will open a graphical interface that displays the performance information.
In your case, I ran the "main.m" file which you mentioned also runs "fpa1.m".
I see the following results by using the profile function for the files that you have attached.
This method can help you find the memory used by the algorithm.
4 commentaires
Walter Roberson
le 10 Fév 2023
There is overhead required to measure execution counts and times for every line and function. That inherently results in profiled code taking longer than if profiling were not on.
Furthermore, when profiling is on, matlab executes the function pretty much as-written. Normally the execution engine is permitted to reorder statements and to substitute high-performance library calls for statements, so the executed code might not be exactly the same as what was written. As an example, a loop that does C(k) = 5*A(k) + 9 might be converted as a whole into a multicore library call that uses streaming hardware instructions. The replacement calls only occur once rather than once per element, but when you are profiling the substitution is not done and you get counted for each execution of the loop body. This is another reason why profiled code takes longer.
Walter Roberson
le 10 Fév 2023
Asking how much memory matlab needs for an algorithm is the wrong question if you are trying to measure how much memory an FPGA would need.
For FPGA if you worrying about memory in the sense of trying to find the smallest FPGA that you can get away with, because larger FPGA "cost more", then although price is a valid concern, smallest is not always least expensive:
- because of economy of scale, the most popular FPGA might be less expensive than the smallest number of gates
- manufacturers compete on speed as well as gates, so potentially an item with a higher number of gates might also have been manufactured with a denser die and run faster
- heat dissipation can reflect manufacturer's design care, so smallest number of gates is not necessarily the coolest running
- smallest number of gates does not necessarily mean highest quality materials
- in the semiconductor industry it has been common to manufacture batches of chips, scrap the ones that fail completely, and test the rest, with the highest performing ones being sold for the highest price, and the lower performing ones sold at lower prices. For systems with on-board memory such as FPGA, it is common for some of the gates to fail in manufacturing, leading the manufacturer to burn out the traces to those gates. The result can then be graded according to number of functioning gates. The FPGA sold as having the smallest capacity is not necessarily one designed around that small capacity: it might instead reflect a larger design that "failed down" to the lower grade. You have to decide whether that is good enough for your purposes. Are these broccoli flowers small because they are quality broccoli harvested young, or because they were what was left over from cutting out the bad parts of larger mediocre broccoli?
6 commentaires
Walter Roberson
le 12 Fév 2023
The memory required to hold the code for the algorithm
MATLAB does not give you any way to calculate that. You can look at the size of the .m file, but that size will include whitespace and space for any comments, MATLAB tokenizes the code when it processes it, so for example
while t < 5
the while is probably going to be represented in some kind of binary code, and there would be some kind of tree data structure, and MATLAB generates machine code, and MATLAB stores debugging information... MATLAB is not a compiler where you could compile to machine code and strip the debugging information and then ask about the size of the output file.
The memory required to input the data
Compare
A = {}
B = 17.8
C = {B}
whos A B C
We know that there must be a symbol table entry for A and that the symbol table must contain the name of the variable and contain information about the size and type of the variable, but whos does not tell us how much space those things take. We can then look at B and see that 8 bytes is recorded, which is exactly the size of one 64 bit double precision floating point number. Now we can look at C, which is a cell that contains a copy of the same number as in B -- in fact in this example, the cell entry will point to exactly the same memory that the data portion of B points to. So the data portion of C contains 8 bytes, leaving 112-8 = 104 bytes. That 108 bytes does not include storage to tell us how big or what type C itself is -- if it did then we would be seeing non-zero bytes for A. So those 104 bytes must be the storage required for the data portion of a symbol table: the data portion of C is a list of pointers to size-and-type-and-data-pointer blocks. We cannot deduce from this how much storage is required to store a variable name but we can deduce from this that 104 bytes is needed to store size and type and data pointers in MATLAB.
Question for you now: does that mean that if you have a scalar value like B in your code, that you should count 112 bytes as the amount of storage it needs? Plus 8 bytes for every additional double precision entry if B is an array? Or should we be counting only the 8 bytes of double precision data?
The memory required to output the data (some algorithms, such as sorting algorithms, do not need memory to output data and usually just rearrange the input data).
In MATLAB, if you have a function of the form
function outvalue = copy(invalue)
outvalue = invalue;
end
then how much space is needed for output? MATLAB uses "copy-on-write" so the size-and-type-and-data-pointer block associated with invalue will be written associated with the symbol outvalue -- using a local symbol table entry but not taking any additional memory otherwise. But if you have
function outvalue = incr(invalue)
outvalue = invalue + 1;
end
then MATLAB has to generate a temporary new size-and-type-and-data-pointer block in order to undertake the invalue + 1 operation (does not overwrite invalue ) and that size-and-type-and-data-pointer block gets associated with the local symbol outvalue so memory is needed for output. We will postpone for a moment the question of how much memory exactly.
Now suppose the code were
function invalue = incr(invalue)
invalue = invalue + 1;
end
The MATLAB language model is to generate a temporary new size-and-type-and-data-pointer block in order to undertake the invalue + 1 operation (not overwriting invalue) and that new size-and-type-and-data-pointer block would be returned.
But... in some limited cases MATLAB can treat the above as an in-place modification, using the same input and output memory. It doesn't typically happen, but it can happen.
Now we have to ask: is the question how much memory MATLAB needs to store and execute this code? Or is the question how much memory would be required by C code generated by the MATLAB to HDL tool ??
Voir également
Catégories
En savoir plus sur MATLAB Functions in Microsoft Excel dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!