 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Tyler Guest
|
Posted: Tue Oct 14, 2003 9:08 pm Post subject: Timing question |
|
|
Hi,
I'm trying to time my C++ program but the standard "time.h" library
only gives precision of up to seconds. I'm looking for greater
precision, maybe in terms of milliseconds or better (i.e. 2.13456 s
for example). Anyone knows how I can go about doing this? Many thanks
in advance.
Tyler
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Jack Klein Guest
|
Posted: Wed Oct 15, 2003 10:46 am Post subject: Re: Timing question |
|
|
On 14 Oct 2003 17:08:38 -0400, [email]hobbit_jr (AT) yahoo (DOT) com[/email] (Tyler) wrote in
comp.lang.c++.moderated:
| Quote: | Hi,
I'm trying to time my C++ program but the standard "time.h" library
only gives precision of up to seconds. I'm looking for greater
precision, maybe in terms of milliseconds or better (i.e. 2.13456 s
for example). Anyone knows how I can go about doing this? Many thanks
in advance.
Tyler
|
There are no standard functions in the C or C++ library that can do
this. Many compilers provide their own particular extensions to
provide higher resolution, but they can be very different from one
compiler/operating system combination to the next.
You really need to ask in a group that supports your particular
compiler and platform to find out what features, if any, if provides.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Sean Fraley Guest
|
Posted: Wed Oct 15, 2003 12:53 pm Post subject: Re: Timing question |
|
|
Tyler wrote:
| Quote: | Hi,
I'm trying to time my C++ program but the standard "time.h" library
only gives precision of up to seconds. I'm looking for greater
precision, maybe in terms of milliseconds or better (i.e. 2.13456 s
for example). Anyone knows how I can go about doing this? Many thanks
in advance.
|
You'll have to go into the OS api for something along that line. Boost has
a library that might be of some use to you.
http://www.boost.org/libs/timer/index.htm
Sean
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Peter van Merkerk Guest
|
Posted: Wed Oct 15, 2003 10:03 pm Post subject: Re: Timing question |
|
|
| Quote: | I'm trying to time my C++ program but the standard "time.h" library
only gives precision of up to seconds. I'm looking for greater
precision, maybe in terms of milliseconds or better (i.e. 2.13456 s
for example). Anyone knows how I can go about doing this? Many thanks
in advance.
|
Possibly the clock() function has a higher resolution on your platform.
If the resolution of clock() function is not sufficient I'm afraid you
will have to resort to platform specific functions.
--
Peter van Merkerk
peter.van.merkerk(at)dse.nl
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Wed Oct 15, 2003 10:36 pm Post subject: Re: Timing question |
|
|
[email]hobbit_jr (AT) yahoo (DOT) com[/email] (Tyler) wrote in message
news:<fb057608.0310140335.247a9c4f (AT) posting (DOT) google.com>...
| Quote: | I'm trying to time my C++ program but the standard "time.h" library
only gives precision of up to seconds. I'm looking for greater
precision, maybe in terms of milliseconds or better (i.e. 2.13456 s
for example). Anyone knows how I can go about doing this? Many thanks
in advance.
|
There's nothing standard for this, so you'll have to check with your
implementation and/or environment. (Under Unix, there is gettimeofday,
which will give you the maximum resolution available -- unless that
resolution is less than a microsecond. I presume that there is
something similar under Windows, and probably under most other systems
as well.)
--
James Kanze GABI Software mailto:kanze (AT) gabi-soft (DOT) fr
Conseils en informatique orientée objet/ http://www.gabi-soft.fr
Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France, +33 (0)1 30 23 45 16
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Siemel Naran Guest
|
Posted: Thu Oct 16, 2003 2:21 pm Post subject: Re: Timing question |
|
|
"Tyler" <hobbit_jr (AT) yahoo (DOT) com> wrote in message
| Quote: | I'm trying to time my C++ program but the standard "time.h" library
only gives precision of up to seconds. I'm looking for greater
precision, maybe in terms of milliseconds or better (i.e. 2.13456 s
for example). Anyone knows how I can go about doing this? Many thanks
in advance.
|
Did you try the function
clock_t clock();
For me, clock_t is long and represents milliseconds. I'm not sure if this
holds across all platforms though.
--
+++++++++++
Siemel Naran
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ben Hutchings Guest
|
Posted: Thu Oct 16, 2003 2:30 pm Post subject: Re: Timing question |
|
|
In article <fb057608.0310140335.247a9c4f (AT) posting (DOT) google.com>, Tyler wrote:
| Quote: | Hi,
I'm trying to time my C++ program but the standard "time.h" library
only gives precision of up to seconds. I'm looking for greater
precision, maybe in terms of milliseconds or better (i.e. 2.13456 s
for example). Anyone knows how I can go about doing this? Many thanks
in advance.
|
clock() normally has resolution smaller than a second (CLOCKS_PER_SEC
specifies just how precise it is). Also, whereas comparing values
returned by time() will tell you the time elapsed, clock() is defined
to return "the implementation's best approximation to" the processor
time used, which in a multitasking and/or multiprocessor system is
of course something different. Note that Microsoft's implementation
of clock() actually returns elapsed time since the start of the
process, just as it would under DOS!
For more precise measurements, you could use the following non-
portable functions:
Elapsed time Processor time
Unix gettimeofday times
Windows GetLocalTime, GetProcessTimes,
GetSystemTime, GetThreadTimes
GetSystemTimeAsFileTime
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Tony Oliver Guest
|
Posted: Thu Oct 16, 2003 2:41 pm Post subject: Re: Timing question |
|
|
[email]hobbit_jr (AT) yahoo (DOT) com[/email] (Tyler) wrote
| Quote: | I'm trying to time my C++ program but the standard "time.h" library
only gives precision of up to seconds. I'm looking for greater
precision, maybe in terms of milliseconds or better (i.e. 2.13456 s
for example). Anyone knows how I can go about doing this? Many thanks
in advance.
|
You shouldn't be using <time.h> - this is a C header file (deprecated
in C++). C++ encapsulates the time API in header <ctime>, which you
should use. In there, you will find clock_t, clock() and
CLOCKS_PER_SEC declared/defined. Put them together like this:
#include <ctime>
#include <iostream>
extern void do_long_process();
int main()
{
clock_t start = clock();
do_long_process();
clock_t finish = clock();
double elapsed_time = double(finish - start) / CLOCKS_PER_SEC;
std::cout << "The long process took "
<< elapsed_time << " seconds."
<< std::endl;
return 0;
}
The granularity of clock() is generally much better than that of
time(). A simple test program can easily detect what that precision
is on your particular target platform.
Hope this helps.
Best regards,
Tony.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Antoun Kanawati Guest
|
Posted: Thu Oct 16, 2003 3:35 pm Post subject: Re: Timing question |
|
|
Tyler wrote:
| Quote: | Hi,
I'm trying to time my C++ program but the standard "time.h" library
only gives precision of up to seconds. I'm looking for greater
precision, maybe in terms of milliseconds or better (i.e. 2.13456 s
for example). Anyone knows how I can go about doing this? Many thanks
in advance.
|
On Win32, the ftime() function yields millisecond resolution.
There's probably something like it on Unix/Linux.
The lazy way is to download Time::HiRes from CPAN, and look
at the source code (it's a Perl module that provides microsecond
resolution, where available). The module is a mixture of
Perl and C.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Thu Oct 16, 2003 8:41 pm Post subject: Re: Timing question |
|
|
In article <4a81d9f8.0310150651.15c035f5 (AT) posting (DOT) google.com>, Tony
Oliver <tony.xt.oliver (AT) ericsson (DOT) com> writes
| Quote: | You shouldn't be using <time.h> - this is a C header file (deprecated
in C++). C++ encapsulates the time API in header <ctime>, which you
should use. In there, you will find clock_t, clock() and
CLOCKS_PER_SEC declared/defined.
|
Using time.h is absolutely fine and will provide you exactly the same
functionality that you get from ctime. The only difference is in regard
to whether namespace std is required explicitly.
--
Francis Glassborow ACCU
If you are not using up-to-date virus protection you should not be
reading
this. Viruses do not just hurt the infected but the whole community.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Alex Vinokur Guest
|
|
| Back to top |
|
 |
Siemel Naran Guest
|
Posted: Fri Oct 17, 2003 11:10 am Post subject: Re: Timing question |
|
|
"Tony Oliver" <tony.xt.oliver (AT) ericsson (DOT) com> wrote in message
| Quote: | #include <ctime
#include
extern void do_long_process();
int main()
{
clock_t start = clock();
|
As you include
std::clock_t start = std::clock();
?
--
+++++++++++
Siemel Naran
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Alex Vinokur Guest
|
Posted: Sat Oct 18, 2003 9:17 am Post subject: Re: Timing question |
|
|
"Tony Oliver" <tony.xt.oliver (AT) ericsson (DOT) com> wrote
[snip]
| Quote: | The granularity of clock() is generally much better than that of
time(). A simple test program can easily detect what that precision
is on your particular target platform.
|
time() and clock() return different values :
* time() returns elapsed (_wall clock_) time,
* clock() returns cumulative _processor_ time.
Here is a sample.
==============================
GNU gcc version 3.3.1 (cygming special)
==============================
========= C++ code : File t.cpp : BEGIN =========
#include <time.h>
#include <unistd.h> // UNIX
#include <assert.h>
#include <iostream>
using namespace std;
int main()
{
time_t start_time, end_time;
clock_t start_clock, end_clock;
clock_t diff_clock;
time_t diff_time;
start_time = time(NULL);
if (start_time == time_t(-1))
{
cout << "Unable to get start time()" << endl;
return 1;
}
start_clock = clock();
if (start_clock == clock_t(-1))
{
cout << "Unable to get start clock()" << endl;
return 1;
}
sleep (3); // UNIX, system call
end_time = time(NULL);
if (end_time == time_t(-1))
{
cout << "Unable to get end time()" << endl;
return 1;
}
end_clock = clock();
if (end_clock == clock_t(-1))
{
cout << "Unable to get end clock()" << endl;
return 1;
}
// -------------------------
assert (!(start_time == (time_t)-1));
assert (!(end_time == (time_t)-1));
assert (!(start_clock == (clock_t)-1));
assert (!(end_clock == (clock_t)-1));
// -------------------------
diff_time = end_time - start_time;
diff_clock = end_clock - start_clock;
cout << endl;
cout << "time : "
<< "start = " << start_time << " sec"
<< ", end = " << end_time << " sec"
<< endl;
cout << "time : "
<< "elapsed (wall clock) time = " << double (diff_time) << " sec"
<< endl;
cout << endl;
cout << "clock : "
<< "start = " << start_clock << " ticks"
<< ", end = " << end_clock << " ticks"
<< endl;
cout << "clock : "
<< "cumulative processor time = " << double(diff_clock)/CLOCKS_PER_SEC << " sec"
<< endl;
return 0;
}
========= C++ code : File t.cpp : END ===========
========= Compilation & Run : BEGIN ===========
$ g++ t.cpp
$ a
time : start = 1066416574 sec, end = 1066416577 sec
time : elapsed (wall clock) time = 3 sec
clock : start = 10 ticks, end = 20 ticks
clock : cumulative processor time = 0.01 sec
========= Compilation & Run : END =============
--
=====================================
Alex Vinokur
mailto:alexvn (AT) connect (DOT) to
http://mathforum.org/library/view/10978.html
news://news.gmane.org/gmane.comp.lang.c++.perfometer
=====================================
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Alex Vinokur Guest
|
Posted: Sat Oct 18, 2003 10:14 pm Post subject: Re: Timing question |
|
|
"Alex Vinokur" <alexvn (AT) bigfoot (DOT) com> wrote
[snip]
| Quote: | * time() returns elapsed (_wall clock_) time,
[snip] |
Sorry,
* `time' -- get current calendar time (as single number) - from 'man time'.
Returned value is number of seconds.
Elapsed (_wall clock_) time is = end_current_calendar_time - start_current_calendar_time.
--
=====================================
Alex Vinokur
mailto:alexvn (AT) connect (DOT) to
http://mathforum.org/library/view/10978.html
news://news.gmane.org/gmane.comp.lang.c++.perfometer
=====================================
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|