5

Goal is to minimize a function over a range of input values. Performance matters. Unfortunately, the ranges::min() algorithm recomputes the output for the live optimum over and over again.

It seems like the algorithm could cache the output value corresponding to the optimum, or am I missing something?

In this example, why does f(x=0) need to be called n times?

#include <ranges>
#include <algorithm>
#include <stdio.h>
using namespace std;

int main()
{
    auto f=[](int x){
        printf("calling f(x=%d)\n", x);
        return x*x;
    };
    auto rg = views::iota(0,4);
    int x1 = ranges::min(rg, {}, f);
}

It outputs:

calling f(x=0)
calling f(x=1)
calling f(x=0)
calling f(x=2)
calling f(x=0)
calling f(x=3)

Is there a way to call ranges::min() in a more optimized way?

Bob__
  • 12,361
  • 3
  • 28
  • 42
Ludovic Aubert
  • 9,534
  • 4
  • 16
  • 28
  • 1
    Memoize pattern maybe? – lorro Dec 05 '22 at 21:13
  • 1
    according to [cppreference](https://en.cppreference.com/w/cpp/algorithm/ranges/min) it is only specified that there are n-1 comparisons, but how often the projection is called seems to be not specified. – 463035818_is_not_an_ai Dec 05 '22 at 21:14
  • Transducers (from Clojure) are also a good option for sequence algorithms that combine for example mapping and reduction. I found this implementation for C++ but haven't tried it: https://github.com/arximboldi/zug . Interesting alternative to some algorithms in the STL. – Rulle Dec 05 '22 at 21:42
  • 1
    fyi https://meta.stackoverflow.com/questions/285551/why-should-i-not-upload-images-of-code-data-errors – 463035818_is_not_an_ai Dec 06 '22 at 08:46

2 Answers2

5

Can ranges::min be implemented in a fashion such that it stores the projected object that it is currently testing against? Well, that would impose specific requirements on such an item. Namely, that you can overwrite it (if you find a smaller element, so that you can cache it in the same variable).

This is not currently a requirement of projection; the result simply has to be comparable with the comparison function. So to impose that would effectively make projection more strict.

Now, an implementation is allowed to cache the value if it so chooses, as projection functors are required to be pure. That is, if it detects that the value type from the projection is copyable, it can copy it. But that's a matter of quality of implementation, not a standard requirement.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
1

There is always the option to compute all outputs first and then locate the min element. But it is a little less memory efficient.

#include <ranges>
#include <algorithm>
#include <vector>
#include <stdio.h>
using namespace std;

int main()
{
    auto f=[](int x){
        printf("calling f(x=%d)\n", x);
        return x*x;
    };
    auto rg = views::iota(0,4);
    vector<int> costs(4);
    transform(ranges::begin(rg), ranges::end(rg), begin(costs), f);
    auto it = ranges::min_element(costs);
    int index = &*it - &costs[0];
}

It outputs:

calling f(x=0)
calling f(x=1)
calling f(x=2)
calling f(x=3)
Ludovic Aubert
  • 9,534
  • 4
  • 16
  • 28