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! ]
> 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
> 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
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
> 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:ka...@gabi-soft.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
> 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
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
> 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.
> 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.
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.
Also getrusage().
[snip]
--
=====================================
Alex Vinokur
mailto:ale...@connect.to
http://mathforum.org/library/view/10978.html
news://news.gmane.org/gmane.comp.lang.c++.perfometer
=====================================
> #include <ctime>
> #include <iostream>
>
> extern void do_long_process();
>
> int main()
> {
> clock_t start = clock();
As you include <ctime> Should that be
std::clock_t start = std::clock();
?
--
+++++++++++
Siemel Naran
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:ale...@connect.to
http://mathforum.org/library/view/10978.html
news://news.gmane.org/gmane.comp.lang.c++.perfometer
=====================================
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.