473,854 Members | 1,511 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

clock() function

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();
/* calculate time in secs */

I have to do this a couple of more times. I'm worried after reading the
man page for clock() which says:
USAGE
The value returned by clock() is defined in microseconds for
compatibility with systems that have CPU clocks with much
higher resolution. Because of this, the value returned will
wrap around after accumulating only 2147 seconds of CPU time
(about 36 minutes).

Does this mean if my code runs more than 36 mins. the timings calculated
will be wrong?

Nov 13 '05
33 47653
Pushkar Pradhan <pu*****@gri.ms state.edu> wrote in message news:<3F******* *******@gri.mss tate.edu>...
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();
/* calculate time in secs */

I have to do this a couple of more times. I'm worried after reading the
man page for clock() which says:
USAGE
The value returned by clock() is defined in microseconds for
compatibility with systems that have CPU clocks with much
higher resolution. Because of this, the value returned will
wrap around after accumulating only 2147 seconds of CPU time
(about 36 minutes).

Does this mean if my code runs more than 36 mins. the timings calculated
will be wrong?


yes. if you want more than that at the resolution that clock()
offers, just write your own timekeeping function, using a combination
of clock() and time(). I've done this before, though i haven't the faintest
clue what I've done with the source, if you have a bash at it, you'll find
that its simple enough

hth
goose,
Nov 13 '05 #11
In <ff************ *************@p osting.google.c om> ru**@webmail.co .za (goose) writes:
yes. if you want more than that at the resolution that clock()
offers, just write your own timekeeping function, using a combination
of clock() and time(). I've done this before, though i haven't the faintest
clue what I've done with the source, if you have a bash at it, you'll find
that its simple enough


I'm afraid that this is not possible at all (in a portable manner) because
there is no connection whatsoever between the times returned by these two
functions.

Feel free to prove me wrong, by posting your code ;-)

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #12

"Daniel Vallstrom" <Da************ **@safelogic.se > wrote in message
news:17******** *************** ***@posting.goo gle.com...
"Mike Wahler" <mk******@mkwah ler.net> wrote in message news:<tF******* *********@newsr ead4.news.pas.e arthlink.net>.. .
"Pushkar Pradhan" <pu*****@gri.ms state.edu> wrote in message
news:3F******** ******@gri.msst ate.edu...
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();
/* calculate time in secs */

I have to do this a couple of more times. I'm worried after reading the man page for clock() which says:
USAGE
The value returned by clock() is defined in microseconds for
compatibility with systems that have CPU clocks with much
higher resolution. Because of this, the value returned will
wrap around after accumulating only 2147 seconds of CPU time
(about 36 minutes).

Does this mean if my code runs more than 36 mins. the timings calculated will be wrong?

[snip]

About your specific question:

If you store your elapsed time in a type 'clock_t'
object then yes, your implementation limits this value
to about 36 minutes before it "wraps" around back
to zero.

But you need not store the value in a type 'clock_t'
object. Use a type with a range large enough for
our anticipated needs, e.g. type 'double'. The required
range of type 'double' is significantly higher than
that of a 31 bit integer value.

double start = (double)clock() ;
double elapsed = 0;

/* etc */

elapsed = (double)clock() - start;


This is no solution. The problem is clock() and just casting its return

values won't magically fix it. You have to keep track of the wraparounds and where clock() is.


Yes, silly me, I realized that *after* I posted. :-)

-Mike
Nov 13 '05 #13
I finally decided to replace clock() since it was causing so much confusion.
I have replaced it with system specific gettimeofday() call which has
enough resolution (microsecs) I think.

Pushkar Pradhan wrote:
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();
/* calculate time in secs */

I have to do this a couple of more times. I'm worried after reading the
man page for clock() which says:
USAGE
The value returned by clock() is defined in microseconds for
compatibility with systems that have CPU clocks with much
higher resolution. Because of this, the value returned will
wrap around after accumulating only 2147 seconds of CPU time
(about 36 minutes).

Does this mean if my code runs more than 36 mins. the timings calculated
will be wrong?


Nov 13 '05 #14

"Robert Stankowic" <pc******@netwa y.at> wrote in message
news:3f******** *************** @newsreader02.h ighway.telekom. at...
Just one afterthought:
I could imagine, that the return value of clock() wraps if clock()
internally uses a counter of type clock_t, but is -1 if clock() internally
uses a type with a higher range.
But that is just a wild guess..
Anyone a more precise statement? Dan Pop, Chris Torek, Richard Heathfield et al !??
I do think that given what the OP's implementation
documents, for it, an algorithm for "wrap adjustment"
could probably be devised, if enough control can
be exercised over the interval between 'clock()' polls.
Maybe, but not easily. clock() measures CPU-time used for the running
process and is AFAICS in no way related or synchronous to time(), so

time() cannot be used to calculate how often clock() wrapped.
No, I wasn't thinking of time(), but of using 'emprical'
knowledge of "expected"
(yes, often a 'dirty word' in programming :-) )
approximate time needed for given
portions of code, to make sure we call 'clock()' frequently
enough. This is what I meant by "if enough control..."
If one wants just to
time a loop, kind of "trial and error" approach may help,
:-)
but for timing a
more complex part of a program, where one cannot easily start with a time
interval which is known to be less than the maximum clock_t value I have no idea.
Again anyone a better statement?


Not this time. Perhaps after I make a second try. :-)
-Mike
Nov 13 '05 #15
Da*****@cern.ch (Dan Pop) wrote in message news:<bm******* ***@sunnews.cer n.ch>...
In <3f************ ***********@new sreader02.highw ay.telekom.at> "Robert Stankowic" <pc******@netwa y.at> writes:

Just one afterthought:
I could imagine, that the return value of clock() wraps if clock()
internally uses a counter of type clock_t, but is -1 if clock() internally
uses a type with a higher range.
But that is just a wild guess..
Anyone a more precise statement?
The words: "the implementation' s best approximation" effectively mean that
an implementation can return *anything* from a clock() call, including a
negative value, without having its conformance affected.

I would hope that an implementation as described by the OP (i.e. most
Unix implementations ) constantly returns -1 after 36 minutes of CPU time,


No! Why would you hope that? To teach those of us who use
wraparounds a lesson?) In fact every system I know of use
wraparounds! This admittedly doesn't include all that many
systems but at least the common ones. It would be interesting
to hear about systems that don't do wraparounds (and at the
same time have a small clock type).

Given that one needs clock timings greater than 36 mins, how should
one achieve that? You could go for a non-C solution. That might get
messy though if you code for many platforms? (I have never chosen
that approach so I wouldn't know.) Or you could assume that clock()
works in the usual and IMO useful way, which IMO includes
wraparounds when they make sense, and use that to make some C
library that is fairly portable.
but I wouldn't bet on that.
I bet that there is a fair amount of C code using that clock() do
wrap around on systems where that has been common practice.
Especially after reading the Linux man page:

BUGS
...
Note that the time can wrap around. On a 32bit system
where CLOCKS_PER_SEC equals 1000000 this function will
return the same value approximately every 72 minutes.

I've never needed to use clock() for intervals longer than a few minutes,
so I've never tested its behaviour on overflow on any implementation.

Most implementations provide better functions for this purpose, e.g. the
Unix times (although it still uses clock_t, the conversion factor is no
longer CLOCKS_PER_SEC, but sysconf(_SC_CLK _TCK), which has a reasonable
value, reflecting the real resolution of these values).
But clock() with wraparounds is adequate, at least if you don't take
into account the nuisance of coding some library to take care of the
wraparounds and clock timings, which only needs to be done once
anyway.
Daniel Vallstrom


Dan

Nov 13 '05 #16
Pushkar Pradhan <pu*****@gri.ms state.edu> writes:
I finally decided to replace clock() since it was causing so much confusion.
I have replaced it with system specific gettimeofday() call which has
enough resolution (microsecs) I think.


<OT>

Note that gettimeofday() gives you the actual wall clock time, whereas
clock() gives you the amount of CPU time used by your program. On any
system with concurrent processes, these can be very different.

As long as you're being system-specific, take a look at the times()
function.

</OT>

--
Keith Thompson (The_Other_Keit h) ks*@cts.com <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 13 '05 #17
In <17************ **************@ posting.google. com> Da************* *@safelogic.se (Daniel Vallstrom) writes:
Da*****@cern.c h (Dan Pop) wrote in message news:<bm******* ***@sunnews.cer n.ch>...
In <3f************ ***********@new sreader02.highw ay.telekom.at> "Robert Stankowic" <pc******@netwa y.at> writes:

>Just one afterthought:
>I could imagine, that the return value of clock() wraps if clock()
>internally uses a counter of type clock_t, but is -1 if clock() internally
>uses a type with a higher range.
>But that is just a wild guess..
>Anyone a more precise statement?


The words: "the implementation' s best approximation" effectively mean that
an implementation can return *anything* from a clock() call, including a
negative value, without having its conformance affected.

I would hope that an implementation as described by the OP (i.e. most
Unix implementations ) constantly returns -1 after 36 minutes of CPU time,


No! Why would you hope that?


Because this is what the standard actually says:

If the processor time used is not available or its value cannot
^^^^^^^^^^^^^^^ ^^^^
be represented, the function returns the value (clock_t)(-1)
^^^^^^^^^^^^^^

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #18

"Dan Pop" <Da*****@cern.c h> wrote in message
news:bm******** **@sunnews.cern .ch...
In <17************ **************@ posting.google. com> Da************* *@safelogic.se (Daniel Vallstrom) writes:
Da*****@cern.c h (Dan Pop) wrote in message news:<bm******* ***@sunnews.cer n.ch>...
In <3f************ ***********@new sreader02.highw ay.telekom.at> "Robert Stankowic" <pc******@netwa y.at> writes:

>Just one afterthought:
>I could imagine, that the return value of clock() wraps if clock()
>internally uses a counter of type clock_t, but is -1 if clock() internally >uses a type with a higher range.
>But that is just a wild guess..
>Anyone a more precise statement?

The words: "the implementation' s best approximation" effectively mean that an implementation can return *anything* from a clock() call, including a negative value, without having its conformance affected.

I would hope that an implementation as described by the OP (i.e. most
Unix implementations ) constantly returns -1 after 36 minutes of CPU
time,
No! Why would you hope that?


Because this is what the standard actually says:

If the processor time used is not available or its value cannot
^^^^^^^^^^^^^^^ ^^^^
be represented, the function returns the value (clock_t)(-1)
^^^^^^^^^^^^^^


Should I understand this to mean that the 'wrapping'
to zero described by the documentation posted by OP
is not an allowed 'implementation-defined' component
of 'clock()' behavior? "Zero" might not be of any
practical use, but perhaps it *could* be called
"best approximation" rather than "not representable".

Are saying that a 'clock()' which "wraps" to zero is
definitely not standard conforming?

-Mike
Nov 13 '05 #19
"Mike Wahler" <mk******@mkwah ler.net> writes:
"Dan Pop" <Da*****@cern.c h> wrote in message
news:bm******** **@sunnews.cern .ch...

[...]
Because this is what the standard actually says:

If the processor time used is not available or its value cannot
^^^^^^^^^^^^^^^ ^^^^
be represented, the function returns the value (clock_t)(-1)
^^^^^^^^^^^^^^


Should I understand this to mean that the 'wrapping'
to zero described by the documentation posted by OP
is not an allowed 'implementation-defined' component
of 'clock()' behavior? "Zero" might not be of any
practical use, but perhaps it *could* be called
"best approximation" rather than "not representable".

Are saying that a 'clock()' which "wraps" to zero is
definitely not standard conforming?


On one system I tested yesterday (Solaris 8), clock_t is a 32-bit
signed integer type. The values returned by the clock() function wrap
around to negative values. That's far more useful than returning -1
after 2147 seconds, but it looks like it violates the standard.

Probably be best solution in this case would be to make clock_t a
64-bit type (as it already is on some platforms).

--
Keith Thompson (The_Other_Keit h) ks*@cts.com <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 13 '05 #20

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

Similar topics

1
3603
by: Agency | last post by:
I'm still working on the bpm counter. I need to have at least 2 displays that are not static. One would be a clock/running time and the other would should the current beat count. How would I do this in Tkinter? I was thinkning of canvas-text, but there must be a widget for doing this kind of display. I'm a bit lost and would appreciate some help.
9
2267
by: Gino Elloso - Philippines | last post by:
Hello, I made a webpage ( @ Geocities Free Webpages ) that contains a mousetrail clock script. I simply copied the script ( including all html tags ) exactly as it was from a source webpage ( that contains only the functioning mousetrail clock ) and pasted it onto mine. But when I try to load the page I made, the clock won't show. Could someone help explain why the clock won't show on my page? Is there a special script for mousetrail...
1
1619
by: s.shahzaib.ali | last post by:
I want to make analog clock skin of windows media player so how can i roteate the layer upto 360 degrees with the help of javascript please tell me iam very anxious about it. iam the first and only skinner in Pakistan. i really need your help. Remember Analog clock Using XML LAYER Wit script of java. you just have tell me about java script i can work with xml well! HAVE A NICE DAY GOOD BYE!
6
61842
by: Charles M. Reinke | last post by:
I'm using the function clock() to measure the run time of a program so that I can compare among several different algorithms. My code looks like: #include <stdio.h> #include <stdlib.h> #include <assert.h> #include <time.h> int main() { clock_t start, stop;
18
3377
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 boils down to the clock() function. Here is the code: #include <iostream> #include <cstdlib> int main(void)
30
17333
by: Matt | last post by:
Does clock_t clock() measure real time or process time? Unless I have missed something, Stroustrup's treatment in TC++PL (section D.4.4.1) is none too clear on that question. Is clock() well-implemented for the major compilers g++, VC++, and Macintosh compilers and other C++ compilers on Linux/Unix, Windows, and MacOS?
2
2835
by: cpptutor2000 | last post by:
Could some C guru suggest some possible solution to my problem? I am trying to simulate a clock and I have tried using 'gettimeofday' and some related C library functions, but I am not getting what I want - the clock should be ticking away on its own, and from time to time, an observer will read the clock value, and decide to do something else. Any hints, suggestions would be greatly appreciated. Thanks in advance for your help.
12
4103
by: cenktarhancenk | last post by:
is there a way to display a ticking clock in a web page using javascript? but not in a textbox, rather as text that i can change the style, font etc. cenk tarhan
9
8309
by: Ron Adam | last post by:
I'm having some cross platform issues with timing loops. It seems time.time is better for some computers/platforms and time.clock others, but it's not always clear which, so I came up with the following to try to determine which. import time # Determine if time.time is better than time.clock # The one with better resolution should be lower.
0
9901
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9752
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11031
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10685
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9518
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7918
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5750
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4162
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3188
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.