# How to find the total memory used during the execution of the code?

47 ビュー (過去 30 日間)
Sadiq Akbar 2023 年 2 月 10 日
コメント済み: Walter Roberson 2023 年 2 月 12 日
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 件のコメント2 件の古いコメントを表示2 件の古いコメントを非表示
Sadiq Akbar 2023 年 2 月 12 日

Sorry I didn't see these statements before. I found the following line in fpa1:
JK=randperm(n);
and replaced it by yours. But I cannot locate the follwoing line in fpa1:
values = min(max(values, Lowerbounds), Upperbounds)
The function simplefounds is used in three different lines. In lines 29 and 34, it is like this:
S(i,:)=simplebounds(S(i,:),Lb,Ub);
In line 53, it is like this:
function s=simplebounds(s,Lb,Ub)
Now which one are you talikg about? You mean that I shoud comment the whole function and use this:
values = min(max(values, Lowerbounds), Upperbounds)
or what should I do?
Walter Roberson 2023 年 2 月 12 日
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

サインインしてコメントする。

### 回答 (2 件)

Dinesh 2023 年 2 月 10 日
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 件のコメント2 件の古いコメントを表示2 件の古いコメントを非表示
Sadiq Akbar 2023 年 2 月 10 日
Thanks a lot for your kind response dear Walter Roberson. Yes, it works. But I don't understand how much memory is used by fpa1? How much time is taken by fpa1. When I run fpa1 whithout profile on, then with tic and toc commands, its time is less but when I run it with profile on, then it takes more time. Why? Or its me that doesn't understand it?
Walter Roberson 2023 年 2 月 10 日
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 2023 年 2 月 10 日
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 件のコメント4 件の古いコメントを表示4 件の古いコメントを非表示
Sadiq Akbar 2023 年 2 月 11 日

Thanks a lot for your kind and detailed responses dear Walter Roberson. You mean that the question, about how much memory, used is basless? You mean that one cannot say that how much memory is used by an algorithm during execution? If, so then why is this:
Walter Roberson 2023 年 2 月 12 日
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 = {}
A = 0×0 empty cell array
B = 17.8
B = 17.8000
C = {B}
C = 1×1 cell array
{[17.8000]}
whos A B C
Name Size Bytes Class Attributes A 0x0 0 cell B 1x1 8 double C 1x1 112 cell
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 ??

サインインしてコメントする。

### カテゴリ

Help Center および File ExchangeLogical についてさらに検索

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by