how to print clock_t variables?

Y

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.
 
D

Dave Vandervies

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
 
P

Peter Pichler

ykz said:
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).
 
E

Eric Sosman

ykz said:
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".
 
D

Dan Pop

In said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top