0

I have a snowball launcher game codes. There is a arm to launch snowball and the target in the game. I have 2 different block of codes that makes the calculation for releasing angle of the arm with the input of length of the arm and target's x and y coordinates. One of them is:

   #include <iostream>
   #include <cmath> // math library
   #include <windows.h> // system()

   using namespace std;

   class SnowballMachine{

  private:
  double L,X,Y, theta;       //L for Lenght of the arm, X and Y is the coordinates 
  const double pi = 3.14159265;         //Theta=Release angle
  public:
 void input()                    //get inputs for lenght of the arm and coordinates
 {
 cout<<"Please enter the coordinations of the target(for Target(x,y) enter 40 28)"   <<endl;
 cin>>X>>Y;
 cout<<"Please enter the length of the arm: "<<endl;
 cin>>L;
  }
  double calculate(){   //calculates the release angle with perpendicular slope comparison
    if(L*Y <= X*sqrt(pow(Y, 2.0)+pow(X, 2.0)-pow(L, 2.0)))
    {
            theta=asin((L*Y + X*sqrt(pow(Y, 2.0)+pow(X, 2.0)-pow(L, 2.0)))/(pow(Y, 2.0)+pow(X, 2.0)));
            return theta;

    }
    else
    {
            theta=asin((L*Y - X*sqrt(pow(Y, 2.0)+pow(X, 2.0)-pow(L, 2.0)))/(pow(Y,2.0)+pow(X, 2.0)));
            return theta;
    }
    }
    void ThetaDisplay()     //displays output
    {
    cout << "The releasing angle is "<<180-(theta*180/pi)<<" degrees"<<endl;
  }
  };

  //const values for options to get input
  const int  OPEN_GATE=1 ;
  const int   LOAD_SNOWBALL = 2;
  const int   ADJUST_ARM=3;
  const int   RELEASE_ARM=4;
  const int   QUIT=5;


 int menu();             // get a command
 void execute(int, SnowballMachine &Dummy);      // run a given command

 int main()
 {

  SnowballMachine A; //calling the class
  A.input();                  //calling the functions
  A.calculate();
  int choice;
  A.ThetaDisplay();
  do                             //select the options
  {
   choice = menu();
   execute(choice, A);
  } while (choice != QUIT );*/
 return 0;
}

int select;
system("cls");
do
{
 cout <<"1....Open the gate\n";
 cout <<"2....Load the Snowball\n";
 cout <<"3...Adjust the arm\n";
 cout <<"4....Release the Snowball\n";
 cout<<"5...Quit\n";
 cout <<"enter selection: ";
 cin >> select;
   } while (select!=1 && select!=2 && select!=3 && select!=4 &&select!=5);
  return select;
 }
void execute(int cmd, SnowballMachine &Dummy)
{
 //options switch method
 switch (cmd)
 {
  case OPEN_GATE: cout << "Opening the gate\n";
        break;
  case LOAD_SNOWBALL: cout << "Loading the snowball\n";
        break;
  case ADJUST_ARM:cout<<"Adjusting the arm\n";
        break;
  case RELEASE_ARM:cout<<"Releasing the arm\n";
        Dummy.calculate();
        Dummy.ThetaDisplay();
        break;
  case QUIT:cout<<"Quitting!!!";
        break;

  default:
    cout <<" Invalid Entry. Try  it again please.";
  }

This is the first one. Second one is: This is the main.cpp

 #include <iostream>
 #include "Snowball.h"

 using namespace std;

 int main()
 {
  Snowball A;
  A.Display();
  A.ArmLength();
  A.Input();
  A.SetStartPOS();

  for(double i = A.getLength(); i>A.getStartPOS(); i-=A.DELTAX)
   {
    if (A.Derivative(A, i)*.9999 >= ((A.getY()-A.foo(i))/(A.getX()-i))*1.0001)
    {
        A.setxPointcirc(i);
        break;
    }
   }

   A.AngleDisplay();

   return 0;

   }

  This is the part of main.cpp which is snowball.cpp which calls all the functions:

  #include "Snowball.h"
  #include <iostream>
  #include <cmath>
  #include <iomanip>

  using namespace std;

  void Snowball::Input()
  {
   cout << "Enter the x-postion of the target: ";
   cin >> x;

  while(x < armlength || x < armlength*-1)
  {
    cout << endl;
    cout << "Please make sure your x is greater than " << armlength << '.' << endl;
    cout << "Enter the x-postion of the target. ";
    cin >> x;

  }
    cout << "Enter the y-postion of the target: ";
    cin >> y;

  while(y < 0 ||  (y < armlength && x<armlength) )
  {
    cout << endl;
    cout << "Enter the y-postion of the target: ";
    cin >> y;
  }
  this->x =x;
  this->y =y;
  }

 void Snowball::ArmLength()
 {
   cout << "Enter the Length of the Arm: ";
   cin >> armlength;
   this->armlength =armlength;
 }
 void Snowball::Display()
 {
   cout << "Welcome to the Snowball Launcher. \n\n";
 }
 double Snowball::foo(double x)
 {
   double z;
   z = sqrt(powf(armlength, 2.0)-powf(x, 2.0));
   return z;
  }
  double Snowball::Derivative(Snowball &foo_dummy, double x)
  {
    return (foo_dummy.foo(x+DELTAX/2.0) - foo_dummy.foo(x-DELTAX/2))/DELTAX;
  }

 void Snowball::AngleDisplay()
 {
    theta = rad2deg(acos(xPointCircle/armlength));
    cout << "\nTarget Destroyed.\nAngle Required is: " << theta << " degrees."  << setprecision(4) <<endl;
  }
  void Snowball::SetStartPOS()
  {
  StartPOS = armlength*-1;
  }
  void Snowball::setxPointcirc(double i)
  {
  xPointCircle = i;
  }

And here is the getters and setters with declaring the const and variables header:

 #ifndef SNOWBALL_H_INCLUDED
 #define SNOWBALL_H_INCLUDED

 #include <iostream>
 #include <cmath>

 using namespace std;

 class Snowball {

 private:
   double rad2deg(double h) {return h*(180/pi); };
   double x, y, theta, xPointCircle, StartPOS, armlength;

 public:
   static const double pi = 3.1415926535897;
   static const double DELTAX = 0.001;

 double foo(double x);
 double Derivative(Snowball &foo_dummy, double x);
 void Display();
 void Input();
 double getLength() {return armlength; }
 double getStartPOS() {return StartPOS; }
 double getY() {return y; }
 double getX() {return x; }
 void setxPointcirc(double i);
 void ArmLength();
 void AngleDisplay();
 void SetStartPOS();
};

#endif

Here is my question: I get the same results with both 2 different block of codes. I want to test which execution time is less(which one would be faster?).

tuygun
  • 79
  • 3
  • 12
  • as you said it yourself: you should test it with a [profiler](http://en.wikipedia.org/wiki/List_of_performance_analysis_tools) – niklasfi Apr 28 '14 at 18:37
  • 3
    https://stackoverflow.com/questions/1861294/how-to-calculate-execution-time-of-a-code-snippet-in-c – Cory Kramer Apr 28 '14 at 18:37
  • In general calculations like this take negligible time compared to other operations like graphics rendering. Trying to find the fastest, before you've identified if this is actually a problem or bottle neck is known as [Premature Optimisation](http://c2.com/cgi/wiki?PrematureOptimization) – dabhaid Apr 28 '14 at 18:42
  • niklasfi- I just downloaded codeTune profile to double-check. Thank you – tuygun Apr 28 '14 at 18:53

1 Answers1

2

Generally the way this is approached is to call the function n number of times (for large n) and calculate the time taken across the calls.

For instance, call it "the first way" 100000 times (getting the time before and time after) then calculate it "the second way" the same number of times (again checking the time before and after). By subtracting the two, you'll get a decent estimate of which is faster/slower.

Note that you need to test it many numbers of times to get an accurate result, not just once!

aardvarkk
  • 14,955
  • 7
  • 67
  • 96
  • Do I have to this test for every single function or it is just for the calculate function? – tuygun Apr 28 '14 at 19:05
  • Just calculate the time for the relevant portion -- the only time you care about is the total execution time for the algorithm. The total time will just be the sum of all of the parts, so just call the "outer" a whole bunch of times and see how long it takes for one vs. the other. – aardvarkk Apr 28 '14 at 19:14