Undestanding proper ctime and clock() usage.

Discussion in 'C++' started by Oplec, Oct 25, 2003.

  1. Oplec

    Oplec Guest

    Hello,

    I am learning C++ using The C++ Programming : Special Edition by Bjarne
    Stroustrup. After reading some of his papers on his site, I came across a
    source code file that is used to compare timings for his Wrapper template
    class. In the main comment section, Mr. Stroustrup indicates that he is
    using a timing method published in JOOP by Andrew Koenig. I searched for the
    article, but was unable to find it. I would like to understand the timing
    method used and certain features of the implementation because so far it has
    eluded me. From what I have read, to measure elapsed time one would:

    #include <ctime>

    clock_t start = clock();
    do_something();
    clock_t end = clock();
    double time_elapsed = double(end - start)/CLOCKS_PER_SEC;

    But in the code file in a number of places a clock_t is recorded and then a
    loop is run until another clock() is passed. I understand that the code is
    measuring the given functions repeatedly until the results are statistically
    relevant, but why clock()'s are added in many places does not make any sense
    to me.

    Do any of you have the article and could explain it to me, or perhaps look
    at the code and explain why so many clock()'s are used?

    The link is: http://www.research.att.com/~bs/wrap_code.cpp

    Thank you for time, Oplec.
     
    Oplec, Oct 25, 2003
    #1
    1. Advertising

  2. "Oplec" <> wrote in message
    news:ISBmb.57188$...
    > From what I have read, to measure elapsed time one would:
    >
    > #include <ctime>
    >
    > clock_t start = clock();
    > do_something();
    > clock_t end = clock();
    > double time_elapsed = double(end - start)/CLOCKS_PER_SEC;


    Yes, this is the basic idea. The code you pointed to just
    does a bunch of additional tricks to try to optimize
    the accuracy of the result.

    Like, for example:
    // Wait for the clock to tick
    clock_t k = clock();
    clock_t start;

    do start = clock();
    while (start == k);
    The point of this code is to wait until the clock just advances,
    to ensure we start measuring time right after a tick (and not
    anywhere between two ticks -- which would mean that only
    some fraction of the first tick interval would be used for
    the code to run).

    Also, other code attempts to subtract the overhead of the
    calls to clock themselves.

    > Do any of you have the article and could explain it to me, or perhaps look
    > at the code and explain why so many clock()'s are used?
    >
    > The link is: http://www.research.att.com/~bs/wrap_code.cpp


    The above should explain all the uses of clock() that I can
    see in the code above.
    Please let me know if something else remains unclear...

    Regards,
    Ivan
    --
    http://ivan.vecerina.com
     
    Ivan Vecerina, Oct 26, 2003
    #2
    1. Advertising

  3. Oplec

    Oplec Guest

    Ivan Vecerina wrote:

    >
    > The above should explain all the uses of clock() that I can
    > see in the code above.
    > Please let me know if something else remains unclear...
    >
    > Regards,
    > Ivan


    The is more understandable for me now. The part where the code measures
    the time taken to "take the time" is interesting. I suppose the original
    author was attempting to achieve the best timing possible. I was
    curious because of other information on how to time used the simpler
    version that I provided as an example. I also like how timings for each
    given function were performed until statistically relevant. I don't know
    much about statistics yet and so was looking up "statistically relevant"
    using Google and didn't find what it was for sure. I guess it means that
    the cumulative average of previous timings for a function is within a
    certain range of the next few timings, that the average should be
    returned as the final result. That makes sense because I used another
    code sample from Mr. Stroustrup, which did not do statistics, and the
    results changed every time that I ran the program. For instance,
    sometimes 0 ms resulted for pointer use, sometimes 14. If it was
    reported statistically like the Wrapper code then I'm sure it would be
    14. With your help I will use the statistics timing code in the future.

    Thank you for your help, Oplec.
     
    Oplec, Oct 26, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Valentin Tihomirov

    Are clock and divided clock synchronous?

    Valentin Tihomirov, Oct 23, 2003, in forum: VHDL
    Replies:
    11
    Views:
    3,300
    louis lin
    Oct 28, 2003
  2. Replies:
    4
    Views:
    724
    Peter Alfke
    Apr 27, 2006
  3. Replies:
    5
    Views:
    2,175
    Ricardo
    Jun 23, 2006
  4. vsgdp

    ctime clock

    vsgdp, May 6, 2007, in forum: C++
    Replies:
    11
    Views:
    752
    Gennaro Prota
    May 7, 2007
  5. himassk
    Replies:
    1
    Views:
    1,236
    Paul Uiterlinden
    May 16, 2007
Loading...

Share This Page