No. of Comparisons

hello guys,

I'm trying to find the number of comparisons a particular program makes. So i decided to make a separate function for comparison but i'm not sure how to go about trying to find the number of times it is called. Any other idea's on how to go about finding the number of comparisons a particular program makes will also be helpful.
Thanks,
The lazy way to do this would be to use a static variable in that function that gets incremented every time the function is called.
But how would i get that counter to the main as compare function is already returning a bool?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool compare(int i, int j, int comparisonMethod)
    { static int x = 0;
       x++;
      if(comparisonMethod ==0) 
          return i==j?true:false;
      if(comparisonMethod ==1) 
          return i>j?true:false;
      if(comparisonMethod ==2)
          return i>=j?true:false;
      if(comparisonMethod ==3) 
          return i<j?true:false;
      if(comparisonMethod ==4) 
          return i<=j?true:false;
    }
Last edited on
You can decide to have your compare method as a struct and overload the bracket operator for comparison

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
struct myCompare {
    bool operator ()(int i, int j, int comparisonMethod) {
        counter++;
        switch (comparisonMethod) {
            case 0: return i == j;
            case 1: return i > j;
            case 2: return i >= j;
            case 3: return i < j;
            case 4: return i <= j;
        }
        return false;
    }
    
    int count() const { return counter; }
    private:
    static int counter;
}compare;

int myCompare::counter = 0;

int main()
{
    compare(3, 4, 1);
    return 0;
}

Well, if you're done changing the parameters of the request after an answer is given; then you'll not want a local static variable. Instead you would pass an integer to the function by reference and increment that.
Thanks smac89
Any chance you could explain the code a bit please? I havent yet learned much about structs or classes.How is the bracket operator overloaded and how would i print the number of counts cout<<counter doesn't seem to work
If you have not yet learned about overloading, then the explanation will not make much sense to you. The bracket operator is basically this "()". You will recognize it as the method of calling a function. So when you write a name and put that operator in front of it, c++ will recognize it as a method call and will proceed to run that method. But you can also overload/define new meaning for these operators. So when I "overloaded" the bracket operator for the myCompare class, this means that you can now call any object of type myCompare with brackets and fill in the proper parameters and the method will execute what ever is in the body of the overloaded operator method.

Simply put, to compare any values you call overloaded bracket operator method (line 2), just the same way I did on line 23. Note the object of type myCompare is defined on line 17.
To find the number of times the method has been called, you call the count method in myCompare class.
@computergeek01
thanks that seems like an eaiser solution but i have 1 question
if the comparisons are spread over a number of functions would i need to declare the Counter as an global variable? as if i intialise the counter = 0 in 1 function. I would not know what value the counter ends up to call it for the next compare in another function.
How would you do this without declaring a global variable as I always have been told its bad to declare global variables(actually why is that ?)
That sounds more like an issue with program flow control. But in case it isn't just keep passing the variable down the line by reference even if you have to go through Function A which doesn't use it but because Function A calls Function B, you pass it anyway. If this is the case either reexamine your program flow or revisit Smac89's solution since it would provide a single data object that would be accessible regardless of scope.

You should do your best to avoid global variables because they make for difficult to read code and they break encapsulation; also, when you get into more complex code, the constructors of objects that require you to call an initialization function would fail depending on where their actual declaration is and that can cause needless pain. But keep in mind that if they were completely useless, then they would have never been included in the language.
Computergeek01
Rethinking and changing the program flow a bit worked
Smac89
Everything makes lot more sense now.
All i ever was taught about overloading was function overloading -_- . Guess a lot more to learn

Thank you very much guys

~void
Topic archived. No new replies allowed.