www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Adding minmaxElement to Phobos?

reply Per =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
At

https://stackoverflow.com/a/19954882/683710

I just learned the reason for C++11 having

https://en.cppreference.com/w/cpp/algorithm/minmax_element

Calculating the min and max simultaneously reduces the number of 
comparisons with 1/4.

Should we add a minmaxElement alongside minElement and maxElement?
Sep 04
next sibling parent jmh530 <john.michael.hall gmail.com> writes:
On Wednesday, 4 September 2019 at 22:12:28 UTC, Per Nordlöw wrote:
 At

 https://stackoverflow.com/a/19954882/683710

 I just learned the reason for C++11 having

 https://en.cppreference.com/w/cpp/algorithm/minmax_element

 Calculating the min and max simultaneously reduces the number 
 of comparisons with 1/4.

 Should we add a minmaxElement alongside minElement and 
 maxElement?
The comments suggest that it doesn't have benefit in some cases. Can't be that hard to do a simple version and see if it has better performance.
Sep 04
prev sibling next sibling parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 9/5/19 12:12 AM, Per Nordlöw wrote:
 At
 
 https://stackoverflow.com/a/19954882/683710
 
 I just learned the reason for C++11 having
 
 https://en.cppreference.com/w/cpp/algorithm/minmax_element
 
 Calculating the min and max simultaneously reduces the number of 
 comparisons with 1/4.
 
 Should we add a minmaxElement alongside minElement and maxElement?
Looks like enough of an principled algorithm to make a valid addition. I think it was an interview question at Facebook at a point :o). (Hmmm... I looked quickly over the C++ sample implementation in the link above and it seems something is amiss.)
Sep 05
parent "Schrom, Brian T" <Brian.Schrom pnnl.gov> writes:
If someone does do this, an interesting comparison point is how it compares to

auto r = reduce!(min, max)(data);

On 9/5/19, 9:37 AM, "Digitalmars-d on behalf of Andrei Alexandrescu via
Digitalmars-d" <digitalmars-d-bounces puremagic.com on behalf of
digitalmars-d puremagic.com> wrote:

    On 9/5/19 12:12 AM, Per Nordlöw wrote:
    > At
    > 
    > https://stackoverflow.com/a/19954882/683710
    > 
    > I just learned the reason for C++11 having
    > 
    > https://en.cppreference.com/w/cpp/algorithm/minmax_element
    > 
    > Calculating the min and max simultaneously reduces the number of 
    > comparisons with 1/4.
    > 
    > Should we add a minmaxElement alongside minElement and maxElement?
    
    Looks like enough of an principled algorithm to make a valid addition. I 
    think it was an interview question at Facebook at a point :o). (Hmmm... 
    I looked quickly over the C++ sample implementation in the link above 
    and it seems something is amiss.)
    
Sep 05
prev sibling parent Gregor =?UTF-8?B?TcO8Y2ts?= <gregormueckl gmx.de> writes:
On Wednesday, 4 September 2019 at 22:12:28 UTC, Per Nordlöw wrote:
 At

 https://stackoverflow.com/a/19954882/683710

 I just learned the reason for C++11 having

 https://en.cppreference.com/w/cpp/algorithm/minmax_element

 Calculating the min and max simultaneously reduces the number 
 of comparisons with 1/4.

 Should we add a minmaxElement alongside minElement and 
 maxElement?
Side note: a stride of two as in the StackOverflow example code would be suboptimal as this should prevent vectorization of the loop. The stride probably needs to be the width of a SIMD register to gain any advantage over the naive loop.
Sep 06