By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,292 Members | 1,642 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,292 IT Pros & Developers. It's quick & easy.

how to print clock_t variables?

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.