# Use of tic toc in MATLAB

## Homework Statement

Calculate the time taken to compute sum of 1/k^3 from 1 -> 1000 using both loops and vectorised commands using tic toc.

## The Attempt at a Solution

tic
n = 1e3; % number of terms in the sum
s = 1; % accumulator variable (for the sum)
for k = 2:n
s = s + 1/k^3;
end
s; % print value of variable s
toc

k = 1:1000;
y = 1./k.^3;
sum(y);
toc

Am I using tic toc in the correct way? If not, how should they be used and if I run this code will the time be displayed in the command window?

DrClaude
Mentor
This will not allow you to independently time both approaches. You have to reset the stopwatch before the vector calculating by using tic again.

By the way, using a real value for n is strange. You should use n=1000. Also, I don't see how
s; % print value of variable s
will print out anything. Any printing should be outside of the timing loop, as it is not part of the algorithm comparison.

This will not allow you to independently time both approaches. You have to reset the stopwatch before the vector calculating by using tic again.

By the way, using a real value for n is strange. You should use n=1000. Also, I don't see how
s; % print value of variable s
will print out anything. Any printing should be outside of the timing loop, as it is not part of the algorithm comparison.

The % was actually an older comment which I never updated! I won't actually be printing out anything. Will using tic toc in the way that I have, and altering it in the way you've pointed out, display the two times to the command window? Also, using a real value for n wasn't my idea at all. It's our lecturers code and the vectorised method of doing the sum is mine. The point of the exercise was to write equivalent code without using loops. Thanks in advance.

DrClaude
Mentor
Will using tic toc in the way that I have, and altering it in the way you've pointed out, display the two times to the command window?
Both will print out two values. In the way you wrote it, the two times you get will be the execution time from the first tic, so to know the timing of the second approach you will have to substract the first time from the second. Adding a tic will give both times independently.

Thanks a million for that. One more question, can you assign the value of the timer to a variable? Like, x = tic or something along those lines. Just so that you could do arithmetic with the two values of the timer if I were to do it my original way? Does that make any sense? I could then output those values to the screen. I'll go ahead and use your way buy just for future reference it'd be handy to know.

DrClaude
Mentor
One more question, can you assign the value of the timer to a variable?
No problem. Check for instance this version of your program:

Code:
tic
n = 1000; % number of terms in the sum
s = 1; % accumulator variable (for the sum)
for k = 2:n
s = s + 1/k^3;
end
loopTime = toc

k = 1:1000;
y = 1./k.^3;
sum(y);
vectTime = toc

vectTime-loopTime
which outputs something like
Code:
loopTime =  0.0032890
vectTime =  0.0033760
ans =  8.7023e-05

Brilliant. Btw, I would try out all these little bits if code in MATLAB itself but I'm currently not in college and don't have the student version. So, thanks for clearing that up!

DrClaude
Mentor
Check out GNU Octave. It is actually the program I was using right now.

Can you run it on Windows?

Thanks for that. Is it very similar to MATLAB?

DrClaude
Mentor
Is it very similar to MATLAB?
Yes and no. I often use it to run matlab code without modification, but it does require extra effort sometimes. The interface is not as sofisticated as that of matlab. But it's free software!

Great. Thanks again!