Undestanding proper ctime and clock() usage.

O

Oplec

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.
 
I

Ivan Vecerina

Oplec said:
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
 
O

Oplec

Ivan said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top