Milliseconds.....

T

Terry Andersen

Can I operate on milliseconds in C? Is there a standard library where I
could retreive the system time in a better resolution than seconds.....? The
time_t gives me only the time like 12:32:11.........
Any ideas.....?

Best Regards
Terry
 
A

Allan Bruce

Terry Andersen said:
Can I operate on milliseconds in C? Is there a standard library where I
could retreive the system time in a better resolution than seconds.....? The
time_t gives me only the time like 12:32:11.........
Any ideas.....?

Best Regards
Terry

have a look at clock()
I think its part of the standard for millisecond resolution
Allan
 
D

Dan Pop

In said:
Can I operate on milliseconds in C?

Maybe. The C standard doesn't specify the resolution of its time
functions, does it?
Is there a standard library where I
could retreive the system time in a better resolution than seconds.....?

The standard library doesn't guarantee any resolution for its time
functions.
The time_t gives me only the time like 12:32:11.........

That's a property of your implementation.
Any ideas.....?

Everything is implementation-specific in this area. If the standard
library functions aren't good enough for your purpose, try to check what
extensions your implementation provides.

On Unix systems, gettimeofday works with microsecond resolution
(if the underlying hardware can actually support such a resolution).

Dan
 
A

Allan Bruce

Richard Bos said:
Not even close. clock() returns the processor time used by the program,
not the current clock time;

You are correct, it has nothing to do with the system time at all.
moreover, you have no guarantee whatsoever
about the precision of its return value.

Is this strictly true? The time.h header also specifies CLOCKS_PER_SEC which
one can use in order to find out the time in seconds. If CLOCKS_PER_SEC is
1000 then we know that the resolution is microsecond.
 
E

Eric G.

Terry Andersen said:
Can I operate on milliseconds in C? Is there a standard library where I
could retreive the system time in a better resolution than seconds.....? The
time_t gives me only the time like 12:32:11.........
Any ideas.....?

You can use C and operate on milliseconds. However, the details of this
would be specific to your machine.

It is possible the timing functions built into your machine, should they
support resolving to milliseconds, are not very accurate, in which case
you will need to look for a 3rd party solution.
 
E

Eric Sosman

Allan said:
You are correct, it has nothing to do with the system time at all.


Is this strictly true? The time.h header also specifies CLOCKS_PER_SEC which
one can use in order to find out the time in seconds. If CLOCKS_PER_SEC is
1000 then we know that the resolution is microsecond.

You mean "millisecond," but it doesn't really matter:
CLOCKS_PER_SEC tells us the units in which `clock_t' expresses
its value, but not the accuracy with which that value is
measured.

("Huh?")

One light-year is the distance a photon travels in one
year in an undisturbed vacuum. The "units" program available
on many Unix systems tells me that this distance is 9.460528e+15
meters. Does that mean that the length of the light-year is
known to an accuracy of plus-or-minus half a meter? Of course
not: it just means that the meter is one of the standard units
in which length is expressed.

On the system I'm using at the moment, CLOCKS_PER_SEC is
one million, meaning that `clock_t' values are expressed to
a precision of one microsecond. But the underlying hardware
clock ticks at 100Hz, so clock() cannot actually measure an
interval shorter than ten milliseconds.

Thought experiment: Express your age as a `clock_t' value
(ignoring possible overflow), using CLOCKS_PER_SEC as it's
defined on your favorite platform. Do you believe the answer?

Precision is one thing, accuracy is another.
 
R

Richard Bos

Allan Bruce said:
You are correct, it has nothing to do with the system time at all.


Is this strictly true? The time.h header also specifies CLOCKS_PER_SEC which
one can use in order to find out the time in seconds. If CLOCKS_PER_SEC is
1000 then we know that the resolution is microsecond.

Yes, this is strictly true, and no, we don't know that. A system that
defines CLOCKS_PER_SEC as 1000, but increases clock() by 10000 every ten
seconds is very poor quality, but legal. More realistically, a system
that defines CLOCKS_PER_SEC as 182, and increases clock() by 10 18.2
times per second is also legal.

Richard
 
D

Dan Pop

Is this strictly true?
Yup!

The time.h header also specifies CLOCKS_PER_SEC which
one can use in order to find out the time in seconds. If CLOCKS_PER_SEC is
1000 then we know that the resolution is microsecond.

Nope, you don't!

fangorn:~/tmp 2298> cat clockres.c
#include <stdio.h>
#include <time.h>

int main()
{
clock_t t1 = clock(), t2;
while ((t2 = clock()) == t1) ;
printf("CLOCKS_PER_SEC: %.0f resolution: %f sec\n",
(double)CLOCKS_PER_SEC, (t2 - t1) / (double)CLOCKS_PER_SEC);
return 0;
}
fangorn:~/tmp 2299> gcc clockres.c
fangorn:~/tmp 2300> ./a.out
CLOCKS_PER_SEC: 1000000 resolution: 0.010000 sec

As you can see, CLOCKS_PER_SEC is merely a conversion factor, it provides
no indication WRT the resolution of the clock() function. The resolution
can only be determined at run time, as shown above.

Dan
 
E

E. Robert Tisdale

Terry said:
Can I operate on milliseconds in C?

There are no standard C library functions
which guarantee resolution down to mulliseconds.
Is there a standard library where I could retrieve the system time
in a better resolution than seconds?

clock_t clock(void);

returns CPU time in seconds/CLOCKS_PER_SEC
but the resolution may be much coarser.
Usually, there is no more than one system clock
that the operating system uses to update system timers
which may be updated no more frequently than
the operating system changes context.
If your time sharing operating has time slices of 0.1 seconds,
the resolution may be no better that 100 milliseconds.
The time_t gives me only the time like 12:32:11.........

Any ideas.....?

Search your operating system documentation
for details of timers available to you.
 
D

Dan Pop

In said:
There are no standard C library functions
which guarantee resolution down to mulliseconds.


clock_t clock(void);

returns CPU time in seconds/CLOCKS_PER_SEC
but the resolution may be much coarser.
Usually, there is no more than one system clock
that the operating system uses to update system timers
which may be updated no more frequently than
the operating system changes context.

This may be true for clock() but shouldn't affect time(), which could
provide the same resolution as the hardware implementing the system's
real time clock (which is slightly less than one microsecond on normal
PC's). The only reason this doesn't happen in practice is that most
implementations provide POSIX semantics for time_t. The standard C
difftime's resolution is theoretically limited only be the representation
of double precision values.

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,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top