Search code examples
c++algorithmc++17c++-chrono

Standard library algorithm to average the difference between adjacent elements


I have a std::vector<std:::chrono::system_clock::time_point> values.

I want to average the time difference in milliseconds between adjacent elements. The container requires at least 2 elements and I am having trouble with the averaging function. I was using std::accumulate but I don;t think I can quite get that to work as I get a ton of compiler errors. The lambda below which I am using as a reduce function with the accumulate algorithm

    auto gLambda = [&](
        system_clock::time_point t1, system_clock::time_point t2) {
            return duration_cast<milliseconds>(t2 - t1).count();
    };

works fine though

The live coliru demo

int main()
{
    // lambda called with adjacent samples from the deque
    auto gLambda = [&](
        system_clock::time_point t1, system_clock::time_point t2) {
            return duration_cast<milliseconds>(t2 - t1).count();
    };
    
    // this line compiles and the lambda seems to do the right thing
    const auto t1 = system_clock::now();
    std::this_thread::sleep_for(55ms);
    const auto t2 = system_clock::now();
    // result is 55 as expected
    auto result = gLambda(t1, t2);    
    std::cout << result << std::endl;    
    // create a vector of 10 timestamps
    std::vector<system_clock::time_point> timeStamps;    
    for (auto i=0; i<10; i++) {
        timeStamps.emplace_back(system_clock::now());
        std::this_thread::sleep_for(15ms);
    }
    // THIS LINE CAUSES THE COMPILATION ERROR
    auto result1 = std::accumulate(timeStamps.cbegin(), timeStamps.cend(), 0.0, gLambda);
    std::cout << result1 << std::endl;
}

results in the following compilation errors that I do not understand.

In file included from /usr/local/include/c++/10.2.0/numeric:62,
                 from main.cpp:4:
/usr/local/include/c++/10.2.0/bits/stl_numeric.h: In instantiation of '_Tp std::accumulate(_InputIterator, _InputIterator, _Tp, _BinaryOperation) [with _InputIterator = __gnu_cxx::__normal_iterator<const std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<long int, std::ratio<1, 1000000000> > >*, std::vector<std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<long int, std::ratio<1, 1000000000> > > > >; _Tp = double; _BinaryOperation = main()::<lambda(std::chrono::_V2::system_clock::time_point, std::chrono::_V2::system_clock::time_point)>]':
main.cpp:30:88:   required from here
/usr/local/include/c++/10.2.0/bits/stl_numeric.h:169:22: error: no match for call to '(main()::<lambda(std::chrono::_V2::system_clock::time_point, std::chrono::_V2::system_clock::time_point)>) (double&, const std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<long int, std::ratio<1, 1000000000> > >&)'
  169 |  __init = __binary_op(_GLIBCXX_MOVE_IF_20(__init), *__first);
      |           ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:12:20: note: candidate: 'main()::<lambda(std::chrono::_V2::system_clock::time_point, std::chrono::_V2::system_clock::time_point)>'
   12 |     auto gLambda = [&](
      |                    ^
main.cpp:12:20: note:   no known conversion for argument 1 from 'double' to 'std::chrono::_V2::system_clock::time_point' {aka 'std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<long int, std::ratio<1, 1000000000> > >'}

Solution

  • As cigien suggested in the comments, here's how to do it with inner_product:

    auto result = inner_product(
                      timeStamps.begin() + 1, timeStamps.end(),
                      timeStamps.begin(), 0ms,
                      [](auto x, auto y) {return x + y;},
                      [](auto x, auto y) {return round<milliseconds>(x - y);})
                  / (timeStamps.size() - 1);