473,396 Members | 1,834 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,396 software developers and data experts.

Milliseconds.....

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
Nov 13 '05 #1
9 12025

"Terry Andersen" <te**@sea.com> wrote in message
news:bg**********@news.net.uni-c.dk...
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
Nov 13 '05 #2
In <bg**********@news.net.uni-c.dk> "Terry Andersen" <te**@sea.com> 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: Da*****@ifh.de
Nov 13 '05 #3

"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:3f*****************@news.nl.net...
"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote:
"Terry Andersen" <te**@sea.com> wrote in message
news:bg**********@news.net.uni-c.dk...
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

Nov 13 '05 #4
Terry Andersen <te**@sea.com> 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... ===
Nov 13 '05 #5
"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote:
"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:3f*****************@news.nl.net...
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
Nov 13 '05 #6
Allan Bruce wrote:

"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:3f*****************@news.nl.net...
"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote:
"Terry Andersen" <te**@sea.com> wrote in message
news:bg**********@news.net.uni-c.dk...
> 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.

--
Er*********@sun.com
Nov 13 '05 #7
In <bg**********@news.freedom2surf.net> "Allan Bruce" <al*****@TAKEAWAYf2s.com> writes:

"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:3f*****************@news.nl.net...
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: Da*****@ifh.de
Nov 13 '05 #8
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.

Nov 13 '05 #9
In <3F**************@jpl.nasa.gov> "E. Robert Tisdale" <E.**************@jpl.nasa.gov> 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: Da*****@ifh.de
Nov 13 '05 #10

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

Similar topics

6
by: Richard Visser | last post by:
Hi, i've found this DateRangeSelector : http://www.petrafex.com/view/pgID/21/db/pages/date_range_selector_date_manip ulation__dates_.html . This works fine but i need it to send the dates as :...
2
by: Kevin Thomas | last post by:
Hello, I have a date value in a table SQL Server database that looks like this: 2005-10-11 12:54:36.860 When I load the record into a dataset in VB.NET and retrieve the date value into code,...
11
by: S_at_work | last post by:
hello, can anybody give me a piece of code , or a hint , how i can recieve the time i need for connect() to another host in milliseconds. My problem is i want test the performance of...
9
by: HL | last post by:
I am using VS 2005 Beta - C# Problem: The Timer fires a few milliseconds before the actual Due-Time Let's say a timer is created in the following manner: System.Threading.Timer m_timer = null;...
2
by: Harlin Seritt | last post by:
How can I take a time given in milliseconds (I am doing this for an uptime script) and convert it to human-friendly time i.e. "4 days, 2 hours, 25 minutes, 10 seonds."? Is there a function from the...
2
by: Rajat | last post by:
Hi, I have to draw a real time chart in which I have several entries at the same second (i.e. HH:MM:SS:Milleconds) The charting component only takes julian date for drawing the dates. I am...
13
by: Sharon | last post by:
I need to test a performance of some code of mine. I tried using the Environment.TickCount but its resolution is too low (from the MSDN remarks: The resolution of the TickCount property cannot be...
2
by: vinodtr | last post by:
Hi all, I have a date which is represented by the number of milliseconds since January 01, 1970. I need to change this back to DATE format. Is there any built in function in DB2 to do the needed...
6
by: Manikandan | last post by:
Hi, I need to insert the datetime with milliseconds value into a datarow. My code as below DataTable testDataTable=new DataTable(); testDataTable.Columns.Add("updatedDateTime",...
16
by: Nathan Sokalski | last post by:
I have a control uses the date and time, including milliseconds. I want to be able to enter this information as an attribute in the *.aspx page. Because all attributes are entered as strings in the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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,...

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.