End line takes large portion of time in Matlab profiler [duplicate] - matlab

So, I've recently started using Matlab's built-in profiler on a regular basis, and I've noticed that while its usually great at showing which lines are taking up the most time, sometimes it'll tell me a large chunk of time is being used on the end statement of a for loop.
Now, seeing as such a line is just used for denoting the end of the loop, I can't imagine how it could use anything other than a trivial amount of processing.
I've seen a specific version of this question asked on matlab central, but a consensus didn't seem to be reached.
EDIT: Here's a minimal example of this problem:
for i =1:1000
x = 1;
x = [x 1];
% clear x;
end
Even if you uncomment the clear, the end line still takes up a lot of computation (about 20%), and the clear actually increases the absolute amount of computation performed by the end line.

When I've seen this in my code, it's been the deallocation of large temporaries created in the loop. Each new variable created in the loop is deallocated at the end.

Related

Matlab GUI windowButtonMotionFcn Slow When No Action Taken

I have a MATLAB (r2016a) GUI with a windowButtonMotionFcn callback. Inside the callback is an if-elseif-else block that changes the cursor depending on which axes in the GUI the mouse is over. It runs very quickly when it encounters the cases when it needs to change something, but according to profiler, takes a significant amount of time on the "return" command.
Can anyone offer insight into a way to correct this? It seems to me if it's not executing any more code, it should be running more quickly when it is exiting the function (i.e. not getting hung up on a "return") than when it has more code to execute.
A basic outline of the code is as follows:
function mouseMove(handles)
xy = %Get cursor position
if %xy over axes1
set(gcf,'Pointer','crosshair')
elseif %xy over axes2
set(gcf,'Pointer','arrow')
else
return %Here is where MATLAB is spending a lot of time
end
%A lot of additional code for when the cursor is over axes1 or axes2.
end
This may be related to the fact that the end of a function often appears in the profiler as taking a lot of time. In general, the profiler is good for identifying inefficiencies in your code (functions that are called too many times, etc.) but really isn't well-suited to actually benchmarking code since JIT acceleration isn't enabled. For benchmarking, it's best to use timeit or some other function.
That being said, when I implement your same callback, I don't see any performance hit for the return statement
As #Ander pointed out, you should pay attention to the Calls column as it's possible that the number of times that return is called is simply more than the rest of the callback. The default coloration of a given line is based on the total time and not the time per call.

Matlab Counter Error Within while loop

I wrote a very simple matlab code which is;
b=4.7;
s=0;
while s <b
s=s+0.1
end
I expect s to be 4.7 but matlab gives 4.8. I am very suprised about this because calculatioun of 0.1 47 times should not give such error. It is a very simple math. Also, if I change b to 4.6 code works fine. I looked the s with format long and matlab gives s = 7.9999999999999. There is a very small error thats why code gives s = 4.8. What is the solution to this problem ? Should I be suspicious about matlab for simple calculations.
I also think your problem is mainly about floating point errors.
Whenever I need it, I use following workaround:
b=4.7;
s=0;
dev = 1e-15; % Maximum deviation (example)
while b-s > dev
s=s+0.1
end
It runs the loop only 47 times and ends it when s = 4.7
It was already indicated that the problem is due to floating point numbers. What you should realize, is that this is not because matlab does something strange, but that other languages that use floating point numbers will encounter exactly the same problem as some numbers simply cannot be stored exactly in this binary format. Here is a simple illustration, that can easily be reproduced in many programming languages:
0.3+0.3+0.3==0.9
This should return true, but unfortunately it returns false.
If you want to be reasonably safe that you do not run into this kind of problem, you should allow for a sufficiently large tolerance. However, you will also want this to be as small as possible to prevent different kinds of problems.
Here is a solution that automatically tries to allow a sensible tolerance:
b=4.7;
s=0;
tol = 100*eps(b);
while s <b-tol
s=s+0.1
end
Note that I would (in general) avoid using a while loop if I already know how often it needs to run. The following code is quite simple and less prone to errors:
for s=0:0.1:4.7
s
end

Show progress of a Matlab instruction execution

Is there any way to show the execution progress (even a rough estimation) of a Matlab instruction?
For example let's say I am computing distances using pdist:
D = pdist(my_matrix,'cosine');
and this computation takes hours, does Matlab provide any way to show periodically the execution progress?
Not intrinsically. You can of course do after-the-fact checking with the profiler or tic/toc.
If this is something you will be doing a lot for a single function, you could consider modifying the function and saving it in your path with a new name (I have a directory called "Modified Builtin" for just this purpose). In the case of pdist.m, you might save pdist_updates.m. Looking a the function, the actual distances are calculated starting around line 250 with a series of nested loops. Add in a line like:
disp(sprintf('Processing pair %d of %d',i,n-1));
at line 265. If you really wanted to get fancy, you could use tic and toc to time each loop and provide an estimate of how long the entire calculation would take so you would know how long you had to run to the coffee machine.
Of course this will cause problems if you end up cancelling your Statistics Toolbox license or if Mathworks upgrades the toolbox and changes the functionality, so use this approach sparingly.

Find Time and Memory after program running in MATLAB

Is it any way in matlab that after program is finished to run, find the memory and time?
Also, if the workplace is saved and then it is loaded again, is it possible to see the time and memory for it ?
Thanks.
For the time consumption, would the profiler work? It slows the execution a bit, but is nice for debugging. Otherwise try to enclose the section you want to time with tic-toc.
And for memory consumption there were, and still is I think, no really convenient way to do this, however, something may have happened here. This is how mathworks answered a few years ago. You can try whos, but that one only works inside the current scope. Also memory can be used to see matlabs total memory consumption.
The time taken for loading a file should be possible to see by enclosing it with the usual tic-toc command. The size of a saved file on disk can be seen using dir on the file, but the size could be different in matlab. I guess that the safest way is to check the size before saving if it will be loaded under the same execution and otherwise it may be convenient to log the size somehow.
Don't know if i got your question correctly, but if you need to trace the time your function takes there are two ways:
the functions
tic;
t=toc
work like a stopwatch, tic starts the counting and toc tells you how long passed since last tic.
if you need to do more in depth analysis of the times matlab also offers a profile function.
i suggest you go through matlab documentation on how to use it...
hope i helped.
S.
For execution time between code lines use:
tic;
toc;
t = toc;
disp(['Execution time: ' num2str(t)])
To know and show memory usage of variables you can use whos
whos
S = whos; % type struct variable containing all the info of the actual workspace
S.bytes
To calculate the total storage, you can make a loop
Memory = 0;
S = whos;
for k = 1:length(S)
Memory = Memory + S(k).bytes;
end
disp(['Total memory used by variables in storage (Bytes): ' num2str(Memory)])
You might prefer to see whos page in mathworks

figures pop up during code is running in matlab

I'm running a large code so I would like to ensure everything is correct when it starts to run. One thing I do is to plot some data to see if they make sense. I had several such plots (figure (1), figure (2) ...) and all of them are buried in different 'if' sentences. But I found only some of them pop up during the code is running, the others just don't show up until the code finished running. I have checked all the if statements are true.
Since my code is a little large I can't put it here. Can someone tell me the possible reasons that affect when figures pop up (during the code is running or after it's finished)? Thanks a lot!
You can use the drawnow function to update your figures while code is still running. If you want your figure window to "pop up" at any given time, you may also need to explicitly call figure(figNum).
Note that while it can be fun to watch your figures update in real time, you may incur a severe performance penalty by doing so. If you have long-running loops, you might consider only showing every N-th update, where N is 10, 100, 1000, or some other appropriate value:
for iter = 1:1e6
plot(x,y);
if ~mod(f0,1000); drawnow; end
end
If you are just interested in plotting right at the beginning as a sanity check, you might run the first iteration of your code outside of a for loop, and run drawnow to make sure it plots. Then enter into the for loop starting at the second iteration. The advantage over testing iter==1 is that you don't have to waste cycles on a conditional at every iteration.