Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Undestanding proper ctime and clock() usage.

Reply
Thread Tools

Undestanding proper ctime and clock() usage.

 
 
Oplec
Guest
Posts: n/a
 
      10-25-2003
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.

 
Reply With Quote
 
 
 
 
Ivan Vecerina
Guest
Posts: n/a
 
      10-25-2003
"Oplec" <(E-Mail Removed)> wrote in message
news:ISBmb.57188$(E-Mail Removed) le.rogers.com...
> 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


 
Reply With Quote
 
 
 
 
Oplec
Guest
Posts: n/a
 
      10-25-2003
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
who do know std::ctime() how implement? Help me? wukexin C++ 1 05-25-2005 01:44 PM
ctime Help Kevin C++ 3 05-16-2004 04:09 PM
Question about ctime Tivo Escobar C++ 1 07-23-2003 07:29 PM
replacement for <ctime>? Ernst Murnleitner C++ 8 07-20-2003 02:40 AM



Advertisments