Use of tic toc in MATLAB

  • Thread starter SherlockOhms
  • Start date
  • Tags
    Matlab
  • #1
310
0

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.


Homework Equations





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?
 

Answers and Replies

  • #2
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
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.
 
  • #4
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.
 
  • #5
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
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
 
  • #7
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
Check out GNU Octave. It is actually the program I was using right now.
 
  • #11
Thanks for that. Is it very similar to MATLAB?
 
  • #12
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!
 
  • #13
Great. Thanks again!
 

Suggested for: Use of tic toc in MATLAB

Replies
5
Views
730
Replies
1
Views
622
Replies
3
Views
455
Replies
10
Views
768
Replies
1
Views
482
Replies
3
Views
616
Replies
1
Views
610
Back
Top