Boost logo

Boost Users :

From: Hung Nguyen Vu (vuhung16plus+shape_at_[hidden])
Date: 2008-07-25 04:56:38


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
<vuhung16plus%7Bremove%7D_at_[hidden]>, YIM: vuhung16 , Skype:
vuhung16dg
Japan through an eye of a gaijin:
http://www.flickr.com/photos/vuhung/tags/fav/


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