Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Milliseconds.....

Reply
Thread Tools

Milliseconds.....

 
 
Terry Andersen
Guest
Posts: n/a
 
      08-06-2003
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


 
Reply With Quote
 
 
 
 
Allan Bruce
Guest
Posts: n/a
 
      08-06-2003

"Terry Andersen" <> wrote in message
news:bgqteb$9oo$...
> 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


 
Reply With Quote
 
 
 
 
Dan Pop
Guest
Posts: n/a
 
      08-06-2003
In <bgqteb$9oo$> "Terry Andersen" <> writes:

>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
--
Dan Pop
DESY Zeuthen, RZ group
Email:
 
Reply With Quote
 
Allan Bruce
Guest
Posts: n/a
 
      08-06-2003

"Richard Bos" <> wrote in message
news:...
> "Allan Bruce" <> wrote:
>
> > "Terry Andersen" <> wrote in message
> > news:bgqteb$9oo$...
> > > 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.....?

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

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

>
> Richard



 
Reply With Quote
 
Eric G.
Guest
Posts: n/a
 
      08-06-2003
Terry Andersen <> wrote:

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



--
== Eric Gorr ========= http://www.ericgorr.net ========= ICQ:9293199 ===
"Therefore the considerations of the intelligent always include both
benefit and harm." - Sun Tzu
== Insults, like violence, are the last refuge of the incompetent... ===
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      08-06-2003
Allan Bruce wrote:
>
> "Richard Bos" <> wrote in message
> news:...
> > "Allan Bruce" <> wrote:
> >
> > > "Terry Andersen" <> wrote in message
> > > news:bgqteb$9oo$...
> > > > 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.....?
> > >
> > > have a look at clock()
> > > I think its part of the standard for millisecond resolution

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


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.

--

 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      08-06-2003
"Allan Bruce" <> wrote:

> "Richard Bos" <> wrote in message
> news:...
> > 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.


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
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      08-06-2003
In <bgr0pn$ik5$> "Allan Bruce" <> writes:


>"Richard Bos" <> wrote in message
>news:...
>
>> moreover, you have no guarantee whatsoever
>> about the precision of its return value.

>
>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
--
Dan Pop
DESY Zeuthen, RZ group
Email:
 
Reply With Quote
 
E. Robert Tisdale
Guest
Posts: n/a
 
      08-06-2003
Terry Andersen wrote:

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

 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      08-07-2003
In <> "E. Robert Tisdale" <> writes:

>Terry Andersen wrote:
>
>> 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.


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
--
Dan Pop
DESY Zeuthen, RZ group
Email:
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57