is my code measuring speed correctly?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • blackx
    New Member
    • Sep 2007
    • 31

    #1

    is my code measuring speed correctly?

    I'm using clock() to measure the speed of my code (testing the speed of passing by value vs passing by reference in function calls). The problem is, the speed returned by my code is always 0.0000000

    Can you check if I'm using the clock() function correctly?

    the function is finding the number in the array that is closest to a number given by the user. Here is the code for the pass by value function.

    Thanks for the help!

    relevant code:
    Code:
    double Array::closestNumber (double num) {
    	double closest = 0;
    	int decimalPlaces = 15;
    	clock_t start;
    	clock_t end;
    	double speed;
    
    	start = clock();
    	for (int i = 0; i < this->length; i++) {
    		if (fabs(this->contents[i] - num) < fabs(closest - num)){
    			closest = this->contents[i];
    		}
    	}
    	end = clock();
    
    
    	speed = ((double)end-(double)start)/CLOCKS_PER_SEC;
    	cout << "The time elapsed is: " << endl;
    	cout << fixed << setprecision(decimalPlaces) << speed <<endl;
    
    	return closest;
    }
  • Firecore
    New Member
    • Jul 2007
    • 114

    #2
    This is the first time i heard of the clock function.
    I did a google search and look at what came up.
    Go here

    I hope this helps.

    Comment

    • blackx
      New Member
      • Sep 2007
      • 31

      #3
      thanks for the reply, yah, I saw that page also. clock() is from the ctime class.

      I kinda got the idea for my code from here:
      http://forums.macrumor s.com/showthread.php? t=360028

      Comment

      • Savage
        Recognized Expert Top Contributor
        • Feb 2007
        • 1759

        #4
        Have you seen this thread?

        Savage

        Comment

        • blackx
          New Member
          • Sep 2007
          • 31

          #5
          no I haven't. Thanks for pointing it out! I'll check it and come back here if I have some questions. Thanks again!

          Comment

          • blackx
            New Member
            • Sep 2007
            • 31

            #6
            okay, I'm back here after reading the thread :)

            I was told to specifically use the clock() function to measure speed. so I couldn't use the info on the other thread.

            Any help would be appreciated. Thanks!

            Comment

            • Ganon11
              Recognized Expert Specialist
              • Oct 2006
              • 3651

              #7
              It looks like everything is in order...except I usually don't cast the clock_t variables to doubles. Try:

              [CODE=cpp]speed = (end - start) / CLOCKS_PER_SEC;[/CODE]

              If this still returns 0, consider the fact that your array might be so small that going though it takes very little time.

              Comment

              • Ganon11
                Recognized Expert Specialist
                • Oct 2006
                • 3651

                #8
                Also, I think I see an error in your logic. When you set closestNumber to 0 initially, there is a chance that this value - 0 - might be the closest to the number, and it isn't even in the array (necessarily)! Try setting closestNumber to the first element in the array, and then checking it against every other element.

                Comment

                • blackx
                  New Member
                  • Sep 2007
                  • 31

                  #9
                  Originally posted by Ganon11
                  It looks like everything is in order...except I usually don't cast the clock_t variables to doubles. Try:

                  [CODE=cpp]speed = (end - start) / CLOCKS_PER_SEC;[/CODE]

                  If this still returns 0, consider the fact that your array might be so small that going though it takes very little time.
                  Hi thanks for the reply.

                  I got the code to work...it seems the reason why I always got 0 IS because the array is too small. So what I did was ran a loop for 10000 times to add up all the speed times, only then did I got some solid numbers to differentiate.

                  2 related questions:
                  1) It seems based on my results that the pass by reference takes longer than pass by value for function calls. This does not seem intuitive to me at all. Can anyone explain why I am getting this result?

                  2) I was trying to do a pass by constant reference

                  and I ended up having to use:
                  Code:
                  double closestNumber (const double& num) const;
                  instead of just:
                  Code:
                  double closestNumber (const double& num);  //no const in the end
                  If I didn't put a const in the end, I get a compile error (ambigious call to overloaded function error c2668). I just would like to understand why I need to use the first type (with the const in the end) for my code to work and what's the difference between the two. I'm still kinda confused with the use of const.

                  Thanks for all the help.

                  Comment

                  • Ganon11
                    Recognized Expert Specialist
                    • Oct 2006
                    • 3651

                    #10
                    When you added the const double& num function, did you get rid of the previous one with double& num in the function header? If not, then you have two functions with very similar signatures. When you call closestNumber(a Number), which one gets called!?

                    Though I'm not sure how adding the 'const' keyword to the function header alleviates the problem, I do know that with that const keyword, you are saying "This function cannot and does not change anything within the class."

                    Comment

                    • blackx
                      New Member
                      • Sep 2007
                      • 31

                      #11
                      Originally posted by Ganon11
                      When you added the const double& num function, did you get rid of the previous one with double& num in the function header? If not, then you have two functions with very similar signatures. When you call closestNumber(a Number), which one gets called!?

                      Though I'm not sure how adding the 'const' keyword to the function header alleviates the problem, I do know that with that const keyword, you are saying "This function cannot and does not change anything within the class."
                      I did get rid of the one without const.
                      Basically, I have only

                      Code:
                      double closestNumber (double num);
                      double closestNumber (const double& num)const;
                      that's why I was confused why the error is for ambiguous overloading...

                      Comment

                      • Ganon11
                        Recognized Expert Specialist
                        • Oct 2006
                        • 3651

                        #12
                        Does the non-const closestNumber change anything within the class? If not, I'd just put to code inside the const version and delete the non-const.

                        Comment

                        Working...