473,404 Members | 2,170 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,404 software developers and data experts.

how to print clock_t variables?

ykz
what format shall i use to print clock_t variables?
i tried %f, %i, %S or %M(seconds or minutes, as said in K&R2 book),
all seem not to work.

the code is like:

#include <time.h>
clock_t t1, t2;
t1 = clock();
/* codes you want to time */
t2 = clock() - t1;
my question is: how to print out the correct value in t2 ?
or am i using clock_t correctly?
thanks for your help.
Nov 13 '05 #1
4 46517
In article <d6**************************@posting.google.com >,
ykz <yk*****@netscape.net> wrote:
what format shall i use to print clock_t variables?
i tried %f, %i, %S or %M(seconds or minutes, as said in K&R2 book),
all seem not to work.

the code is like:

#include <time.h>
clock_t t1, t2;
t1 = clock();
/* codes you want to time */
t2 = clock() - t1;
my question is: how to print out the correct value in t2 ?
Since you're probably more interested in the amount of time taken than
the number of clock ticks, it's probably best to divide the value by
CLOCKS_PER_SEC and print that value, suitably converted:
--------
double seconds;
/*clock_t is required to be an arithmetic type, so this is valid.
It may be possible for a sufficiently perverse implementation to break
the result, though (f'rexample, a C99 implementation could store
important information in the imaginary part of a complex type,
which the conversion to a real type discards)
*/
time_in_seconds=(double)time_in_clock_ticks/(double)CLOCKS_PER_SEC;
printf("%g seconds",seconds);
--------

or am i using clock_t correctly?


As long as you're aware that clock() measures CPU time, not wall time,
and that the tick time is likely to be rather longer than the time it
takes any reasonable simple operation to be done once, and that the timer
resolution could be less than the tick time, and you're sure that none of
these will affect the validity of your results, you're using it correctly.
dave

--
Dave Vandervies dj******@csclub.uwaterloo.ca
I think he should instead be tortured by being forced to sit in a comfortable
armchair whilst someone brings him fiendishly tasty coffee and biscuits. THAT
will teach him to mess with comp.lang.c! HA! --Richard Heathfield in CLC
Nov 13 '05 #2
ykz wrote:

what format shall i use to print clock_t variables?
i tried %f, %i, %S or %M(seconds or minutes, as said in K&R2 book),
all seem not to work.

the code is like:

#include <time.h>
clock_t t1, t2;
t1 = clock();
/* codes you want to time */
t2 = clock() - t1;

my question is: how to print out the correct value in t2 ?
or am i using clock_t correctly?
thanks for your help.


There are multiple layers of obscurity and opacity here,
since all the Standard says about `clock_t' is that it is an
"arithmetic type." Different implementations are free to use
different types, so there's no format specifier that is sure
to work on all implementations.

Furthermore, the precision[*] of a `clock_t' value is
also implementation-dependent; you need to divide by the
implementation-defined value `CLOCKS_PER_SEC' to convert
it to seconds. The division is just a little bit tricky,
though, because `CLOCKS_PER_SEC' is also a `clock_t' value,
and if the implementation has chosen some flavor of integer
as its `clock_t', the division will be an integer division
and will discard any fraction.

Some people therefore recommend an explicit conversion
to `double' before doing the division, and then printing
the quotient (in seconds) with some kind of "%f" specifier:

printf ("%.2f\n", (double)t2 / CLOCKS_PER_SEC);

However, it is just barely possible that this could be
wrong. `clock_t' could be *any* arithmetic type -- and
if the implementation uses `long double' as its `clock_t',
not only would the conversion specifier need to be "%Lf"
instead of "%f", but there's also the fear that chopping
the `long double' down to `double' might lose precision
or even be invalid altogether. So the next refinement is

printf ("%.2Lf\n", (long double)t2 / CLOCKS_PER_SEC);

.... but this is also unsatisfactory, to me at least. Using
all the potentially gruesome machinery of `long double' just
to cater to an unlikely "corner case" is irksome. I finally
settled on this scheme:

printf ("%.2f\n",
(double)( (t2 + 0.0) / CLOCKS_PER_SEC) );

The `t2 + 0.0' piece promotes the numerator to at least
`double', and leaves it at `long double' if that's what a
`clock_t' is anyhow. Then the division is carried out in
`double' or `long double'. Finally, the quotient (now in
seconds) is converted to `double' if it was `long double'
or left as `double' if that's what it already was, and the
`double' result is then printed with "%f".

--
Er*********@sun.com
Nov 13 '05 #3
"ykz" <yk*****@netscape.net> wrote in message
news:d6**************************@posting.google.c om...
what format shall i use to print clock_t variables?
i tried %f, %i, %S or %M(seconds or minutes, as said in K&R2 book),
all seem not to work.

the code is like:

#include <time.h>
clock_t t1, t2;
t1 = clock();
/* codes you want to time */
t2 = clock() - t1;

my question is: how to print out the correct value in t2 ?
or am i using clock_t correctly?
thanks for your help.


The Standard is rather vague about clock_t. It merely states that it is
"arithmetic type capable of representing time". This sounds OK to me as
long as you do arithmetics only, but for printing I would convert clock_t
to double and used %f (or even better, %.f).

Nov 13 '05 #4
In <d6**************************@posting.google.com > yk*****@netscape.net (ykz) writes:
what format shall i use to print clock_t variables?
What's the point in printing clock_t variables?
i tried %f, %i, %S or %M(seconds or minutes, as said in K&R2 book),
all seem not to work.
Where does K&R2 recommend %S or %M as valid printf conversion descriptors?
the code is like:

#include <time.h>
clock_t t1, t2;
t1 = clock();
/* codes you want to time */
t2 = clock() - t1;

my question is: how to print out the correct value in t2 ?
My question is: what is the meaning of t2?
or am i using clock_t correctly?


Once you convert t2 to a meaningful value, just use a conversion
descriptor that is appropriate for the type of that value. The most
common way to get a meaningful value out of t2 is

double interval = t2 / (double)CLOCKS_PER_SEC;

Now, it's obvious that %f is the right conversion descriptor for the
interval variable.

Note that clock() may not be suitable for very small or very large time
intervals. For best results, try to keep the interval between 1 and 1000
seconds.

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

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

Similar topics

23
by: Mark Parnell | last post by:
I'm relatively new to PHP, and have just converted a site from ASP to PHP. There is one thing I haven't managed to do, though. When the site was using ASP, I had one file (called variables.asp),...
7
by: Phi | last post by:
Hi there, I've just started learning php and I hope you guys could help me with this problem. I've found a useful pice of code from a book to make a form and I would like to use it. I would like...
13
by: Larry L | last post by:
I have a Module that declares several arrays as public, some string, some integers. I then have 2 forms. Form A is the main form, that loads on start-up, and has a command button to open Form B. On...
27
by: gabor | last post by:
hi, as far as i know in python there aren't any private (i mean not accessible from the outside of the object) methods/fields. why? in java/c++ i can make a method private, this way...
30
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()...
9
by: s99999999s2003 | last post by:
hi say i have variables like these var1 = "blah" var2 = "blahblah" var3 = "blahblahblah" var4 = "...." var5 = "...".. bcos all the variable names start with "var", is there a way to
41
by: none | last post by:
Hello, IIRC, I once saw an explanation how Python doesn't have "variables" in the sense that, say, C does, and instead has bindings from names to objects. Does anyone have a link? Thanks, ...
4
by: icarus | last post by:
global_vars.py has the global variables set_var.py changes one of the values on the global variables (don't close it or terminate) get_var.py retrieves the recently value changed (triggered right...
4
by: hausburn | last post by:
The Thankyou(); function will not print the variables fname and lname to screen. the variables are extracted from a file. #include <cstdlib> #include <iostream> #include <fstream> #include...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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,...
0
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...
0
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,...
0
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...
0
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...
0
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...
0
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,...
0
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...

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.