Boost logo

Boost Users :

Subject: [Boost-users] [accumulator] std::chrono::duration in boost::accumulator
From: georg_at_[hidden]
Date: 2017-02-12 09:01:56


I want to build a histogram of
`std::chrono::high_resolution_clock::duration` object. For this issue i
wanted to use boost::accumulators as the framework.

As it did not compile in my project i teared it down and based it on a
running example with `double` as a data type.

My general goal on my project is to do use for everything united types.
Time and all measured stuff. I would want to avoid to use `.count()` on
the duration.

Is there any known workaround around this?

it is not dependent on the compiler as it seems. With gcc 6.3 it failes too.

It compiles when i use

    clang++-3.9 --std=c++14 -g -O0 main.cpp

But when i set the define USE_CHRONO it failes with the error:

    clang++-3.9 --std=c++14 -g -O0 -DUSE_CHRONO main.cpp
    In file included from main.cpp:7:
    /usr/include/boost/accumulators/statistics/density.hpp:75:15: error:
no matching constructor for initialization of 'array_type' (aka
'vector<std::chrono::duration<unsigned long, std::ratio<1, 1000000000>
> >')
                , samples_in_bin(num_bins + 2, 0.)
                  ^ ~~~~~~~~~~~~~~~~

and here is my teared down example. I used this site as a base and
exhanged double to data_t.
http://programmingexamples.net/wiki/CPP/Boost/Histogram

    #include <vector>
    #include <algorithm>
    #include <iostream>
    #include <chrono>

    #include <boost/accumulators/accumulators.hpp>
    #include <boost/accumulators/statistics/density.hpp>
    #include <boost/accumulators/statistics/stats.hpp>

    using namespace boost;
    using namespace boost::accumulators;

    using duration_t = std::chrono::high_resolution_clock::duration;
    #ifdef USE_CHRONO
    using data_t = duration_t;
    data_t construct(double d)
    {
            return std::chrono::milliseconds(0);
    }
    #else
    using data_t = double;
    data_t construct(double d)
    {
            return d;
    }

    #endif

    using acc = accumulator_set<data_t, features<tag::density> >;
    using histogram_type = iterator_range<std::vector<std::pair<data_t,
data_t> >::iterator >;

    template <typename T>
    class data_filler
    {
    public:
      data_filler(){}
      T operator()() { return rand()/(T)RAND_MAX; }
    };

    template <>
    class data_filler<duration_t>
    {
    public:
      data_filler(){}
      duration_t operator()() { return
std::chrono::milliseconds(rand()/RAND_MAX); }
    };

    int main(int argc, char** argv)
    {

      //create some random data
      std::vector<data_t> data(100);
      std::generate(data.begin(), data.end(), data_filler<data_t>());
      int c = data.size();//cache size for histogramm.

      //create an accumulator
      acc myAccumulator( tag::density::num_bins = 20,
tag::density::cache_size = 10);

      //fill accumulator
      for (int j = 0; j < c; ++j)
      {
        myAccumulator(data[j]);
      }

      auto hist = density(myAccumulator);

      data_t total = construct(0.0);

      for( int i = 0; i < hist.size(); i++ )
      {
    // std::cout << "Bin lower bound: " << hist[i].first << ", Value: "
<< hist[i].second << std::endl;
        total += hist[i].second;
      }

      //std::cout << "Total: " << total << std::endl; //should be 1 (and
it is)

      return 0;
    }


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