1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Use of tic toc in MATLAB

  1. Oct 2, 2013 #1
    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. jcsd
  3. Oct 3, 2013 #2

    DrClaude

    User Avatar

    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.
     
  4. Oct 3, 2013 #3
    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.
     
  5. Oct 3, 2013 #4

    DrClaude

    User Avatar

    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.
     
  6. Oct 3, 2013 #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.
     
  7. Oct 3, 2013 #6

    DrClaude

    User Avatar

    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
     
     
  8. Oct 3, 2013 #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!
     
  9. Oct 3, 2013 #8

    DrClaude

    User Avatar

    Staff: Mentor

    Check out GNU Octave. It is actually the program I was using right now.
     
  10. Oct 3, 2013 #9
    Can you run it on Windows?
     
  11. Oct 4, 2013 #10

    DrClaude

    User Avatar

    Staff: Mentor

  12. Oct 4, 2013 #11
    Thanks for that. Is it very similar to MATLAB?
     
  13. Oct 4, 2013 #12

    DrClaude

    User Avatar

    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!
     
  14. Oct 4, 2013 #13
    Great. Thanks again!
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Use of tic toc in MATLAB
Loading...