执行时间比较

时间:2014-04-28 18:34:35

标签: c++ algorithm

我有一个雪球发射器游戏代码。有一只手臂可以在游戏中发射雪球和目标。我有2个不同的代码块,用于计算臂的释放角度,输入臂的长度和目标的x和y坐标。其中之一是:

   #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.";
  }

这是第一个。第二个是: 这是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;
  }

这里是getter和setter,声明了const和variables头:

 #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

这是我的问题:我用两个不同的代码块得到了相同的结果。我想要 测试哪个执行时间较少(哪一个会更快?)。

1 个答案:

答案 0 :(得分:2)

一般来说,接近它的方法是多次调用函数(对于大n)并计算调用所用的时间。

例如,称之为&#34;第一种方式&#34; 100000次(获得之前的时间和之后的时间)然后计算它&#34;第二种方式&#34;相同的次数(再次检查前后的时间)。通过减去这两个,你可以得到一个合适的估计,其中更快/更慢。

请注意,您需要多次测试才能获得准确的结果,而不仅仅是一次!