Several time counters in MATLAB

I have a program running a loop I want to have two time counters, one for the loop, that will tell me how log did one iteration of the loop took, and one for the entire program. To the best of my knowledge tic and toc will work only once.

Answers


You're only familiar with this tic toc syntax:

tic; someCode; elapsed = toc;

But there is another syntax:

start = tic; someCode; elapsed = toc(start);

The second syntax makes the same time measurement, but allows you the option of running more than one stopwatch timer concurrently. You assign the output of tic to a variable tStart and then use that same variable when calling toc. MATLAB measures the time elapsed between the tic and its related toc command and displays the time elapsed in seconds. This syntax enables you to time multiple concurrent operations, including the timing of nested operations (matlab documentation of tic toc).

Here's how to use it in your case. Let's say that this is your code:

for i = 1:M
   someCode;
end

Insert the tic and toc like this:

startLoop = tic;
for i = 1:N
   startIteration = tic;
   someCode;
   endIteration = toc(startIteration);
end
endLoop = toc(startLoop);

You can also use the above syntax to create a vector for which the ith element is the time measurement for the ith iteration. Like this:

startLoop = tic;
for i = 1:N
   startIteration(i) = tic;
   someCode;
   endIteration(i) = toc(startIteration(i));
end
endLoop = toc(startLoop);

You can use tic and toc to time nested operations, from the Matlab help for tic:

tStart=tic; any_statements; toc(tStart); makes the same time measurement, but allows you the option of running more than one stopwatch timer concurrently. You assign the output of tic to a variable tStart and then use that same variable when calling toc. MATLAB measures the time elapsed between the tic and its related toc command and displays the time elapsed in seconds. This syntax enables you to time multiple concurrent operations, including the timing of nested operations


I'm not able to try this right now, but you should be able to use multiple tic and toc statements if you store the tic values into variables.

Read Matlab's documentation on this, there is even a section on nesting them. Here is a rough example:

tStartOverall = tic;

...

tStartLoop = tic;
<your loop code here>
tEndLoop = toc(tStartLoop);

...

tEndOverall = toc(tStartOverall);

Need Your Help

Change text color based on brightness of the covered background area?

javascript jquery html5 canvas background

I've thought about the following for a while already, so now I want to know your opinions, possible solutions, and so on.

Validating dynamically loaded choices in Symfony 2

symfony validation symfony-2.1

I have a choice field type named *sub_choice* in my form whose choices will be dynamically loaded through AJAX depending on the selected value of the parent choice field, named *parent_choice*. Loa...