[accumulator] std::chrono::duration in boost::accumulator

Previous Topic Next Topic
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

[accumulator] std::chrono::duration in boost::accumulator

Boost - Users mailing list
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.

    #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);
    using data_t = double;
    data_t construct(double d)
            return d;


    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
      T operator()() { return rand()/(T)RAND_MAX; }

    template <>
    class data_filler<duration_t>
      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)

      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 mailing list
[hidden email]