Use of tic toc in 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
DrClaude
Mentor
7,554
3,893
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
310
0
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
DrClaude
Mentor
7,554
3,893
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
310
0
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
DrClaude
Mentor
7,554
3,893
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
310
0
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
DrClaude
Mentor
7,554
3,893
Check out GNU Octave. It is actually the program I was using right now.
 
  • #9
310
0
Can you run it on Windows?
 
  • #11
310
0
Thanks for that. Is it very similar to MATLAB?
 
  • #12
DrClaude
Mentor
7,554
3,893
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
310
0
Great. Thanks again!
 

Related Threads on Use of tic toc in MATLAB

  • Last Post
Replies
8
Views
651
  • Last Post
Replies
2
Views
2K
Replies
5
Views
4K
Replies
11
Views
8K
  • Last Post
Replies
8
Views
7K
Replies
1
Views
3K
Replies
1
Views
4K
Replies
5
Views
4K
Replies
0
Views
4K
Replies
1
Views
1K
Top