Say you have an array of 2D points and are looking to find the min/max bounding box.
Which would be faster, the manual approach:
float min_x = min_y = highest, max_x = max_y = lowest;
for(auto p: points) {
max_x = max(max_x, p.x);
max_y = max(max_y, p.y);
min_x = min(min_x, p.x);
min_y = min(min_y, p.y);
}
Or using C++ tools:
auto[min_x, max_x] =
minmax_element(values.begin(), values.end(), [](auto p1, auto p2) { return p1.x < p2.x; });
auto[min_y, max_y] =
minmax_element(values.begin(), values.end(), [](auto p1, auto p2) { return p1.y < p2.y; });
I am wondering about which should be, in theory, faster. I don't care about the amount of time in ms that it takes in a specific machine to finish, I want to know which of these 2 I should expect to be faster, prior to bench marking.
Always plump for the function from the C++ Standard Library:
Because it's extremely well-specified and tied to a particular compiler, that compiler can recognise the C++ Standard Library function and make optimisations. Perhaps the function is even hardcoded into the compiler?
minmax_element was introduced since its result can be found in one traversal of the data. (In fact the standard does not allow it to make two separate traversals.)
I would bet for the first option, assuming that four statements of kind aN += bN are first independent and secondly they can be parallelized, combined, and auto vectorized. And it of course helps, when there exists a single instruction opcode for min/max, as in x64/simd architecture. In this context, conditionally skipping the other comparison can be an example of premature optimization. Moreover, traversing an array only once should make a difference for large arrays due to memory access costing generally more than ALU operations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With