# Use of tic toc in MATLAB

1. Oct 2, 2013

### SherlockOhms

1. The problem statement, all variables and given/known data
Calculate the time taken to compute sum of 1/k^3 from 1 -> 1000 using both loops and vectorised commands using tic toc.

2. Relevant equations

3. 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?

2. Oct 3, 2013

### Staff: 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.

3. Oct 3, 2013

### SherlockOhms

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.

4. Oct 3, 2013

### Staff: Mentor

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.

5. Oct 3, 2013

### SherlockOhms

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.

6. Oct 3, 2013

### Staff: Mentor

No problem. Check for instance this version of your program:

Code (Text):
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 (Text):
loopTime =  0.0032890
vectTime =  0.0033760
ans =  8.7023e-05

7. Oct 3, 2013

### SherlockOhms

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!

8. Oct 3, 2013

### Staff: Mentor

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

9. Oct 3, 2013

### SherlockOhms

Can you run it on Windows?

10. Oct 4, 2013

### Staff: Mentor

11. Oct 4, 2013

### SherlockOhms

Thanks for that. Is it very similar to MATLAB?

12. Oct 4, 2013

### Staff: Mentor

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!

13. Oct 4, 2013

### SherlockOhms

Great. Thanks again!