473,839 Members | 1,540 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 47649
In <ff************ **************@ posting.google. com> ru**@webmail.co .za (goose) writes:
Da*****@cern.c h (Dan Pop) wrote in message news:<bm******* ***@sunnews.cer n.ch>...
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.


no, there isn't. but it is possible, at the start of the program,
to *establish* a point of reference for the 2 tmies returned by
these functions.


What do you mean?
once you have a base reference, and you know
how many times clock() wraps around in a certain time interval,
(say, once every 36 mins), then you can always use time(), check
If your code is relying on such information, then it is no longer
portable.
if the return minus the last return of time() is greater than
the wraparound time, and then use clock() to get the finer granularity.
There is NO way to combine time() and clock(), as they return completely
different times. Imagine that your program is running together with 9
other programs, all 10 being CPU-bound. The time interval reported by
time() will be about 10 times as large as the time interval reported by
clock().

Things are even hairier if the system load is variable during the
interval (which is quite common in practice, when sharing a system with
other people).
its not clean, its not neat, but it is possible, no ?


Is it? You have produced no *valid* argument and no code.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #31
>>> >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.
no, there isn't. but it is possible, at the start of the program,
to *establish* a point of reference for the 2 tmies returned by
these functions.


What do you mean?
once you have a base reference, and you know
how many times clock() wraps around in a certain time interval,
(say, once every 36 mins), then you can always use time(), check


If your code is relying on such information, then it is no longer
portable.
if the return minus the last return of time() is greater than
the wraparound time, and then use clock() to get the finer granularity.


There is NO way to combine time() and clock(), as they return completely
different times. Imagine that your program is running together with 9
other programs, all 10 being CPU-bound. The time interval reported by
time() will be about 10 times as large as the time interval reported by
clock().


You don't need to combine them. Check clock() and time() periodically.
Compute the difference between the last call to clock() and this
one, and add it to a running sum in a variable with enough range
to handle the sum with reasonable precision and range (e.g. long
double, or use a bignum package). Compute the difference between
the last call to time() and this one. If it's longer than the
fastest possible wraparound time (e.g. 36 minutes above), you *MIGHT*
have blown it and your result should be considered possibly inaccurate.
If it's not, you should have reasonable accuracy.
Things are even hairier if the system load is variable during the
interval (which is quite common in practice, when sharing a system with
other people).


In the above situation it doesn't get hairier, but you might
actually get an accurate answer even if you're not sure it is accurate.

Now, about computing the wraparound time: you know that difftime()
returns the difference of two times in units of seconds, and you
know that the difference of two clock_t values is in units of
(1.0/CLOCKS_PER_SEC) seconds. Further, it is a reasonable guess
that if clock_t is signed (which you can test at runtime) and if
sizeof(clock_t) is equal to one (or more) of sizeof(short),
sizeof(int), sizeof(long), or sizeof(long long), the maximum value
of a clock_t is very likely equal to the corresponding SHRT_MAX,
INT_MAX, LONG_MAX, or LLONG_MAX. If it's unsigned you can determine
the maximum value by setting a clock_t variable to (clock_t)-1,
then assigning that to some variable with bigger range, such as
long double. Now, how much is that maximum value divided by
CLOCKS_PER_SEC in seconds? It's OK if you guess low.

Now, you might get in trouble if your program can be heavily
multi-threaded (no, I'm not talking about a thread-aware program,
I'm talking about the compiler dividing the program into threads
on its own) by a smart compiler (and is running on, for example,
a kilo-processor array) and clock_t time in seconds goes by faster
than time_t time.

Gordon L. Burditt
Nov 13 '05 #32
In <bn********@lib rary2.airnews.n et> go***********@s neaky.lerctr.or g (Gordon Burditt) writes:
You don't need to combine them. Check clock() and time() periodically.
What exactly "periodical ly" means? How do you know when it's time to
check them?
Compute the difference between the last call to clock() and this
one, and add it to a running sum in a variable with enough range
to handle the sum with reasonable precision and range (e.g. long
double, or use a bignum package). Compute the difference between
the last call to time() and this one. If it's longer than the
fastest possible wraparound time (e.g. 36 minutes above), you *MIGHT*
have blown it and your result should be considered possibly inaccurate.
If it's not, you should have reasonable accuracy.
Things are even hairier if the system load is variable during the
interval (which is quite common in practice, when sharing a system with
other people).


In the above situation it doesn't get hairier, but you might
actually get an accurate answer even if you're not sure it is accurate.

Now, about computing the wraparound time: you know that difftime()
returns the difference of two times in units of seconds, and you
know that the difference of two clock_t values is in units of
(1.0/CLOCKS_PER_SEC) seconds.


Doesn't help much if the wraparound time is less than one second, does it?

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #33
Dan Pop wrote:
In <bn********@lib rary2.airnews.n et> go***********@s neaky.lerctr.or g
(Gordon Burditt) writes:
You don't need to combine them. Check clock() and time() periodically.


What exactly "periodical ly" means? How do you know when it's time to
check them?


You can find out when it's time to check them by checking clock() and time()
periodically.

<g,d&r>

--
Richard Heathfield : bi****@eton.pow ernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #34

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

Similar topics

1
3602
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
61840
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
3376
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
17327
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
4102
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
9855
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
9697
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
10906
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...
1
10647
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10292
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7017
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5866
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4482
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4064
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.