Re: [Boost-users] Boost.Date_Time - How to measure time elapsed?

I've been struggling with how to use the Date_Time library to measure time elapsed (like to measure how long code takes to execute).
I would suggest using boost::timer instead. It's quite simple: #include <boost/timer.hpp> boost::timer t; // code to be measured.... Double e = t.elapsed(); t.restart(); // code to be measured.... e = t.elapsed();

Hello,
I would suggest using boost::timer instead. It's quite simple:
Quite simple but quite limited, for portability reasons. I personally no longer use it since I've discovered it can't run over 35 minutes on my platform. It's also far from having the microsecond resolution requested here. Bruno

----- Original Message ----- From: "Bruno Lalande" <bruno.lalande@gmail.com> To: <boost-users@lists.boost.org> Sent: Thursday, July 24, 2008 10:51 AM Subject: Re: [Boost-users] Boost.Date_Time - How to measure time elapsed?
The request was mili seconds (msec). Vicente

The request was mili seconds (msec).
Yes you're right, but it doesn't provide that resolution either (at least on my platform, where the resolution is 0.01 second). If I've understood well, the resolution depends on what std::clock provides so it can't be guaranteed. Bruno

Hi,
What platform is that? Cheers, Andrej __________________________________________________________ Not happy with your email address?. Get the one you really want - millions of new email addresses available now at Yahoo! http://uk.docs.yahoo.com/ymail/new.html

What platform is that?
A very common one: Linux 2.6 with GCC 4.1 or 4.2. The distribution is Kubuntu. timer().elapsed_max() gives 2147.48 on this platform and indeed, elapsed() returns negative numbers above that time (before returning to 0). I had thought about the possibility of making a policy-based version of boost::timer, with something like a "heart policy" that would define the way in which the time would be obtained and stored internally. There would be a portable one (the one of boost::timer currently), another one based on boost::date_time, and why not some platform-specific ones (QueryPerformanceCounter on windows for instance). But I never had the time to dig more :-( Bruno

On Fri, Jul 25, 2008 at 5:29 PM, Bruno Lalande <bruno.lalande@gmail.com> wrote:
I don't mean to hijack the thread but please let me share another way to use timer, with C, not C++ or boots. In the code, not only the time of one test, but average time, and its standard derivation are calculated so we will know how "close" is the average time. My question: Is there any similar implementation in boost? The code is as follows: int count = 30; long long times[count]; for (i = 0; i < count; i++) { time start; gettimeofday (&start, NULL); // do something gettimeofday (&end, NULL); long long time = ((end.tv_sec - start.tv_sec) * 1000000 + (end.tv_usec - start.tv_usec)); times[i] = time; } // and use it printf("mean: %f, stddev: %f\n", mean(times, 20), stddev(times, 20); double variance (long long *x, int n) { double m = x[0]; double s = 0; int i; for (i = 0; i < n; i++) { double m_new = m + (x[i] - m) / (i + 1); s = s + (x[i] - m) * (x[i] - m_new); m = m_new; } return s / (n - 1); } double mean (long long *x, int n) { double m = x[0]; int i; for (i = 0; i < n; i++) { m = m + (x[i] - m) / (i + 1); } return m; } double stddev (long long *x, int n) { return sqrt (variance (x, n)); } -- Best Regards, Nguyen Hung Vu ( Nguyễn Vũ Hưng ) vuhung16plus{remove}@gmail.dot.com <vuhung16plus%7Bremove%7D@gmail.dot.com>, YIM: vuhung16 , Skype: vuhung16dg Japan through an eye of a gaijin: http://www.flickr.com/photos/vuhung/tags/fav/

On Fri, Jul 25, 2008 at 5:29 PM, Bruno Lalande <bruno.lalande@gmail.com> wrote:
I don't mean to hijack the thread but please let me share another way to use timer, with C, not C++ or boots. In the code, not only the time of one test, but average time, and its standard derivation are calculated so we will know how "close" is the average time. My question: Is there any similar implementation in boost? The code is as follows: int count = 30; long long times[count]; for (i = 0; i < count; i++) { time start; gettimeofday (&start, NULL); // do something gettimeofday (&end, NULL); long long time = ((end.tv_sec - start.tv_sec) * 1000000 + (end.tv_usec - start.tv_usec)); times[i] = time; } // and use it printf("mean: %f, stddev: %f\n", mean(times, 20), stddev(times, 20); double variance (long long *x, int n) { double m = x[0]; double s = 0; int i; for (i = 0; i < n; i++) { double m_new = m + (x[i] - m) / (i + 1); s = s + (x[i] - m) * (x[i] - m_new); m = m_new; } return s / (n - 1); } double mean (long long *x, int n) { double m = x[0]; int i; for (i = 0; i < n; i++) { m = m + (x[i] - m) / (i + 1); } return m; } double stddev (long long *x, int n) { return sqrt (variance (x, n)); } -- Best Regards, Nguyen Hung Vu ( Nguyễn Vũ Hưng ) vuhung16plus{remove}@gmail.dot.com <vuhung16plus%7Bremove%7D@gmail.dot.com>, YIM: vuhung16 , Skype: vuhung16dg Japan through an eye of a gaijin: http://www.flickr.com/photos/vuhung/tags/fav/

I don't know it this helps, but I think it is getting system time to a reasonable precision on Windows and Linux platforms, I use two doubles representing the proleptic Julian day Number and the fraction of the day: #ifdef _MSC_VER struct __timeb64 timebuffer; struct tm *sysUTC = new tm; #if _MSC_VER < 1400 // Visual Studio 7.1 and earlier. _ftime64( &timebuffer ); sysUTC = _gmtime64( &timebuffer.time ); #else _ftime64_s( &timebuffer ); _gmtime64_s( sysUTC, &timebuffer.time ); #endif sec = sysUTC->tm_sec + double(timebuffer.millitm) / 1000.0; double sysDate = JulianDayNumber(sysUTC->tm_year+1900,(char)sysUTC->tm_mon+1,(char)sysUTC->tm_mday); double sysFoD = ToFoD(sysUTC->tm_hour,(char)sysUTC->tm_min,sec); delete sysUTC; #else struct timeval ltime; gettimeofday(<ime,NULL); // Obtain UTC from the computer. struct tm sysUTC; gmtime_r( <ime.tv_sec, &sysUTC ); sec = sysUTC.tm_sec+ltime.tv_usec/1000000.0; double sysDate = JulianDayNumber(sysUTC.tm_year+1900,(char)sysUTC.tm_mon+1,(char)sysUTC.tm_mday); double sysFoD = ToFoD(sysUTC.tm_hour,(char)sysUTC.tm_min,sec); #endif Andrew On Fri, Jul 25, 2008 at 6:57 PM, Nguyen Vu Hung <vuhung16plus@gmail.com> wrote:
-- ___________________________________________ Andrew J. P. Maclean Centre for Autonomous Systems The Rose Street Building J04 The University of Sydney 2006 NSW AUSTRALIA Ph: +61 2 9351 3283 Fax: +61 2 9351 7474 URL: http://www.acfr.usyd.edu.au/ ___________________________________________
participants (7)
-
Andrej van der Zee
-
Andrew Maclean
-
Bruno Lalande
-
Hung Nguyen Vu
-
Nguyen Vu Hung
-
Tan, Tom (Shanghai)
-
vicente.botet