Boost logo

Boost Users :

From: Andrew Maclean (andrew.amaclean_at_[hidden])
Date: 2008-07-26 01:22:01


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(&ltime,NULL);
  // Obtain UTC from the computer.
  struct tm sysUTC;
  gmtime_r( &ltime.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_at_[hidden]> wrote:
>
>
> On Fri, Jul 25, 2008 at 5:29 PM, Bruno Lalande <bruno.lalande_at_[hidden]>
> wrote:
>>
>> > 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 :-(
>
> 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 , YIM: vuhung16 , Skype: vuhung16dg
> Japan through an eye of a gaijin:
> http://www.flickr.com/photos/vuhung/tags/fav/
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>

-- 
___________________________________________
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/
___________________________________________

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net