473,382 Members | 1,692 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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.

Jul 19 '05 #1
2 19462
"Oplec" <an*******@anonymous.cp> wrote in message
news:IS*******************@news01.bloor.is.net.cab 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
Jul 19 '05 #2
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.

Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Kevin | last post by:
Hello: I am trying to find documentation on using the ctime library as I need to be able to instantiate an object to keep track of the time when a certain event occurs. I also need to be able...
5
by: berkay | last post by:
long num,num1; char *first; char *last; num=ilk.getTime();//gets time first=ctime(&num); cout.flush(); fflush(stdin); 1) cout<<"first:"<<first;
33
by: Pushkar Pradhan | last post by:
I'm using clock() to time parts of my code e.g. clk1 = clock(); /* code */ clk2 = clock(); /* calculate time in secs */ ...... clk1 = clock(); /* code */ clk2 = clock();
18
by: roberts.noah | last post by:
Ok, so I am trying to establish how boost.timer works and I try a few things and afaict it doesn't. So I open the header to see why and run some tests to establish what is going on and it all...
8
by: B Williams | last post by:
I have been searching the internet trying to find the definition for the function time() in the standard library header <ctime>. Can someone help me with this? Thanks in advance.
11
by: vsgdp | last post by:
Hi, I was wondering if the clock function is reliable for long periods of time (maybe up to 10 hours). The reason I ask is because Stroustrup says "the clock() function is meant to measure...
8
by: Pietro Cerutti | last post by:
Hi to all, I'm just curious about the rationale of having ctime() put a new line character at the end of its return value, as: "Thu Nov 24 18:22:48 1986\n\0" Personally, I find it stupid and...
4
by: Pietro Cerutti | last post by:
Hi group, #include <stdio.h> #include <unistd.h> #include <time.h> int main(void) { time_t t1, t2; char *st1, *st2;
9
by: Lars Uffmann | last post by:
Is the ctime library really that poorly designed, or is it just me looking to do things the wrong way? The whole concept of the struct tm seems way too highlevel for my taste - this is something...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.