| View previous topic :: View next topic |
| Author |
Message |
Partho Choudhury Guest
|
Posted: Mon Jun 28, 2004 12:18 pm Post subject: System time in C++ |
|
|
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
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
marbac Guest
|
Posted: Mon Jun 28, 2004 12:40 pm Post subject: Re: System time in C++ |
|
|
Partho Choudhury wrote:
| Quote: | 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 ("%dn",n);
wait (1);
}
printf ("FIRE!!!n");
return 0;
}
Regards marbac
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Mon Jun 28, 2004 1:24 pm Post subject: Re: System time in C++ |
|
|
Partho Choudhury wrote:
| Quote: | 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
|
|
| Back to top |
|
 |
Evan Carew Guest
|
Posted: Mon Jun 28, 2004 9:34 pm Post subject: Re: System time in C++ |
|
|
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Partho,
You could try using the boost::osix_time::ptime interface. it is quite
simple.
http://www.boost.org/libs/date_time/doc/index.html#usage
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iD8DBQFA4BbEoo/Prlj9GScRAkdvAJ455/WvFDB3qy3to8hvF9vnWcJZZACdFGMU
uzE86LIai4MH49d6+rdyhgs=
=HWtn
-----END PGP SIGNATURE-----
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Alan Johnson Guest
|
Posted: Tue Jun 29, 2004 9:37 am Post subject: Re: System time in C++ |
|
|
Partho Choudhury wrote:
| Quote: | 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
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Daniel Pfeffer Guest
|
Posted: Tue Jun 29, 2004 9:42 am Post subject: Re: System time in C++ |
|
|
"Partho Choudhury" <partho (AT) ieee (DOT) org> wrote
| Quote: | 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
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
John Potter Guest
|
Posted: Tue Jun 29, 2004 7:05 pm Post subject: Re: System time in C++ |
|
|
On 29 Jun 2004 05:42:47 -0400, "Daniel Pfeffer" <pfefferd (AT) hotmail (DOT) co.il>
wrote:
| Quote: | 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
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Brian McGuinness Guest
|
Posted: Tue Jun 29, 2004 7:22 pm Post subject: Re: System time in C++ |
|
|
[email]partho (AT) ieee (DOT) org[/email] (Partho Choudhury) wrote in message news:<3dec3df8.0406280144.982c420 (AT) posting (DOT) google.com>...
| Quote: | 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
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Tommi Mäkitalo Guest
|
Posted: Tue Jun 29, 2004 9:24 pm Post subject: Re: System time in C++ |
|
|
....
| Quote: |
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.
|
|
| Back to top |
|
 |
Julie Guest
|
Posted: Tue Jun 29, 2004 9:59 pm Post subject: Re: System time in C++ |
|
|
Tommi Mäkitalo wrote:
| Quote: |
...
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.
|
In C++, there is *no* other way to do it.
Yielding is all hardware/os/thread specific.
|
|
| Back to top |
|
 |
Tommi Mäkitalo Guest
|
Posted: Wed Jun 30, 2004 7:03 am Post subject: Re: System time in C++ |
|
|
Julie wrote:
| Quote: | Tommi Mäkitalo wrote:
...
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.
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
--
|
|
| Back to top |
|
 |
|