System time in C++

P

Partho Choudhury

Hi all:

I need to add a snippet which access the system time (upto atleast
milliseconds) using ANSI std. C++. I cannot use MFC and Win32 APIs in
my program for now for various reasons. Is there something like this
in ANSI C++ which allows access up to milliseconds. I know that time.h
and CTime allow access up to the second level, but I need to work in
the msec level.

Any help will be appreciated.

Partho
 
M

marbac

Partho said:
Hi all:

I need to add a snippet which access the system time (upto atleast
milliseconds) using ANSI std. C++. I cannot use MFC and Win32 APIs in
my program for now for various reasons. Is there something like this
in ANSI C++ which allows access up to milliseconds. I know that time.h
and CTime allow access up to the second level, but I need to work in
the msec level.



Hi,

In time.h there should be something like this:

/* This defines CLOCKS_PER_SEC, which is the number of processor clock
ticks per second. */
# include <bits/time.h>

/* This is the obsolete POSIX.1-1988 name for the same constant. */
# if !defined __STRICT_ANSI__ && !defined __USE_XOPEN2K
# ifndef CLK_TCK
# define CLK_TCK CLOCKS_PER_SEC
# endif
# endif

with clock () you can get the clock ticks since start. As an example
(source:cplusplus.com):


/* clock example: countdown */
#include <stdio.h>
#include <time.h>

void wait ( int seconds )
{
clock_t endwait;
endwait = clock () + seconds * CLK_TCK ; // or CLOCKS_PER_SEC
while (clock() < endwait) {}
}

int main ()
{
int n;
printf ("Starting countdown...\n");
for (n=10; n>0; n--)
{
printf ("%d\n",n);
wait (1);
}
printf ("FIRE!!!\n");
return 0;
}

Regards marbac
 
V

Victor Bazarov

Partho said:
I need to add a snippet which access the system time (upto atleast
milliseconds) using ANSI std. C++. I cannot use MFC and Win32 APIs in
my program for now for various reasons. Is there something like this
in ANSI C++ which allows access up to milliseconds. I know that time.h
and CTime allow access up to the second level, but I need to work in
the msec level.

So, your question is essentially this: I know that the language doesn't
have a mechanism to do A, but is there a way in the language to do A?

BTW, there is no standard symbol 'CTime'. Perhaps you're confusing C++
with MFC (which you cannot use for various reasons).

Repeat after me: there is no standard C++ way to access system time
except by using the functions in <ctime> (or <time.h>). If you need
a millisecond resolution (or better), you _have_ to use OS-specific
mechanisms if they are available.

V
 
A

Alan Johnson

Partho said:
Hi all:

I need to add a snippet which access the system time (upto atleast
milliseconds) using ANSI std. C++. I cannot use MFC and Win32 APIs in
my program for now for various reasons. Is there something like this
in ANSI C++ which allows access up to milliseconds. I know that time.h
and CTime allow access up to the second level, but I need to work in
the msec level.

Any help will be appreciated.

As others have said, this isn't possible. [Offtopic: A somewhat
portable way would be to use the POSIX ftime function, which I believe
exists on Windows as _ftime.]

Alan
 
D

Daniel Pfeffer

Partho Choudhury said:
Hi all:

I need to add a snippet which access the system time (upto atleast
milliseconds) using ANSI std. C++. I cannot use MFC and Win32 APIs in
my program for now for various reasons. Is there something like this
in ANSI C++ which allows access up to milliseconds. I know that time.h
and CTime allow access up to the second level, but I need to work in
the msec level.

Nothing in the Standard guarantees that _any_ clock is available. For
example, it is within the Standard to have clock() return (time_t)0 for all
calls. I agree that such an implementation would be a lousy one, but that is
a matter of quality of implementation, not the Standard.

Most C++ systems will provide a clock with resolution of 1 second, but
support of millisecond resolution is far from universal.

The CLOCKS_PER_SEC macro (defined in <time.h>) gives the number of ticks per
second, so you can give a compile-time message that the environment does not
meet the minimum requirements.

If you are willing to use other standards in addition to the C++ standard,
then IIRC - POSIX mandates a clock with millisecond resolution.


HTH
Daniel Pfeffer
 
J

John Potter

The CLOCKS_PER_SEC macro (defined in <time.h>) gives the number of ticks per
second, so you can give a compile-time message that the environment does not
meet the minimum requirements.

The macro only gives the unit of recording not the unit of change. It
is common to have CLOCKS_PER_SEC 1000000 indicating that the unit of
measurement is microseconds and actual changes in steps of 10000 giving
measurements in centiseconds. The macro tells nothing about accuracy.
It is only useful for converting the value to seconds.

John
 
B

Brian McGuinness

Hi all:

I need to add a snippet which access the system time (upto atleast
milliseconds) using ANSI std. C++. I cannot use MFC and Win32 APIs in
my program for now for various reasons. Is there something like this
in ANSI C++ which allows access up to milliseconds. I know that time.h
and CTime allow access up to the second level, but I need to work in
the msec level.

Any help will be appreciated.

Partho


I don't know how widely supported it is, but you could try the
clock_gettime() function. This supports resolutions up to nanoseconds,
though of course this depends on the quality of the system clock. See
http://www.opengroup.org/onlinepubs/009695399/basedefs/time.h.html

I see this function defined in the Solaris 7 time.h file.

--- Brian
 
T

Tommi =?UTF-8?B?TcOka2l0YWxv?=

....
void wait ( int seconds )
{
clock_t endwait;
endwait = clock () + seconds * CLK_TCK ; // or CLOCKS_PER_SEC
while (clock() < endwait) {}
}
....
Dont't do that! You eat the whole cpu for nothing.
 
T

Tommi =?ISO-8859-1?Q?M=E4kitalo?=

Julie said:
In C++, there is *no* other way to do it.

Yielding is all hardware/os/thread specific.
Yes there are other ways to do it in c++. You can call system-specific
routines very easyly. ::sleep(unsigned int) conforms to POSIX as I read in
my man-page. It is just a little less portable than ::clock(), which is
part of ANSI-C.

Tommi
--
 

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,774
Messages
2,569,598
Members
45,151
Latest member
JaclynMarl
Top