Undestanding proper ctime and clock() usage.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Oplec

    Undestanding proper ctime and clock() usage.

    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.

  • Ivan Vecerina

    #2
    Re: Undestanding proper ctime and clock() usage.

    "Oplec" <anonymous@anon ymous.cp> wrote in message
    news:ISBmb.5718 8$h61.34769@new s01.bloor.is.ne t.cable.rogers. com...[color=blue]
    > 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;[/color]

    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.
    [color=blue]
    > 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[/color]

    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
    --
    Ivan Vecerina - expert in medical devices, software - info, links, contact information, code snippets



    Comment

    • Oplec

      #3
      Re: Undestanding proper ctime and clock() usage.

      Ivan Vecerina wrote:
      [color=blue]
      >
      > 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[/color]

      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 "statistica lly 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.

      Comment

      Working...