Comparison of 5 integers in least number of comparisons

I have this code and need to make sure all the resulting variables are equal?

long result1 = timer.CalculateElapsedTimeInMinutes();
long result2 = timer.CalculateElapsedTimeInMinutes();
long result3 = timer.CalculateElapsedTimeInMinutes();
long result4 = timer.CalculateElapsedTimeInMinutes();
long result5 = timer.CalculateElapsedTimeInMinutes();

      

This is what I did, but I feel like it can be done in an easier way, maybe not?

bool allEqual = (result1 == result2) && (result3 == result4) && (result1 == result3) && (result1 == result5);

      

Thank.

+2
c # algorithm


source to share


9 replies


It's just possible that there is some hacky / clever way that can be done with XOR or something, but your code makes it clear what you want to do and will still be ridiculously fast. The chances of this becoming a bottleneck are close enough to 0 not to be considered IMO - so go with the most readable code.

I would be more consistent in your comparisons:



bool allEqual = (result1 == result2) && 
                (result1 == result3) && 
                (result1 == result4) &&  
                (result1 == result5);

      

It's easier to see visually that you have good reason, IMO.

+14


source to share


No, you need at least n-1 comparisons. Although I would write it like this:



bool allEqual = (result1 == result2)
             && (result2 == result3)
             && (result3 == result4)
             && (result4 == result5);

      

+5


source to share


No, that's very good.

Everything should be made as simple as possible, but not simpler. - Albert Einstein

+2


source to share


You can also do this with reference using the All extension method.

        var results = new long[] {
            timer.CalculateElapsedTimeInMinutes(),
            timer.CalculateElapsedTimeInMinutes(),
            timer.CalculateElapsedTimeInMinutes(),
            timer.CalculateElapsedTimeInMinutes(),
            timer.CalculateElapsedTimeInMinutes()
        };

        bool allEqual = results.All(x => x == results[0]);

      

+2


source to share


The only way it could be faster is short-circuiting in such a way that a pair that was probably different is evaluated first.

+1


source to share


Just for this, will this work?

  bool allequal =
       res1 & res2 & res3 & res4 & res5 == 
       res1 | res2 | res3 | res4 | res5; 

      

just one comparison ... <grin>

(unless you count bitwise operations!)

+1


source to share


How about this with LINQ:

var firstValue = timer.CalculateElapsedTimeInMinutes();

var list = new List<long>();
list.Add(timer.CalculateElapsedTimeInMinutes());
list.Add(...);

bool allEqual = list.All(i => i == firstValue);

      

0


source to share


It depends on what you mean by comparison. You can do this using just one explicit comparison operator:

bool result = 
    ((result1 ^ reesult2) | (result1 ^ result3) | (result1 ^ result4) | (result1 ^ result5))==0;

      

It may even take advantage of the generated code - code that uses boolean and should stop performing comparisons as soon as it finds any non-equal value. This means that each comparison must be done individually and it must include code to complete testing after each comparison.

Unless you're doing this inside a really complex loop, however, it's not worth what you think is most readable.

0


source to share


You can also make a helper function for this:

bool AllEqual<T>(params T[] values) where T : IEquatable<T> {
    // make a decision here and document it
    if (values.Length < 1) return true; // I guess?

    bool result = true;
    T first = values[0];

    for (int i = 1; i < values.Length; i++) {
        result = result && first.Equals(values[i]);
    }

    return result;
}

      

0


source to share







All Articles
Loading...
X
Show
Funny
Dev
Pics