Thread: Could this be done better ?

  1. #1
    Registered User big146's Avatar
    Join Date
    Apr 2003
    Posts
    74

    Could this be done better ?

    I was wondering if there was any one in here that could tell me if this code could be done better or faster. Take a look.
    Code:
    void VectorAngles( const float *forward, float *angles )
    {
    	float	tmp, yaw, pitch;
    	
    	if (forward[1] == 0 && forward[0] == 0)
    	{
    		yaw = 0;
    		if (forward[2] > 0)
    			pitch = 90.0;
    		else
    			pitch = 270.0;
    	}
    	else
    	{
    		yaw = (atan2f(forward[1], forward[0]) * 57.3f);
    	
    		
    		if (yaw < 0) yaw += 360.0;
    
    		tmp = sqrt (forward[0]*forward[0] + forward[1]*forward[1]);
    		
    		pitch = (atan2f(forward[2], tmp) * 57.3f);
    		
    		}
    
    		angles[0] = pitch;
    		angles[1] = yaw;
    		angles[2] = 0;
    }
    big146

  2. #2
    Registered User
    Join Date
    Feb 2004
    Posts
    35
    You might want to let the functions take the floats independently, or even use structs, so you don't accidentally pass an undersized pointer/array and get GPFs. Also, if you name them appropriately (IE *X, *Y, *Z, P, Y), it will be much easier to see what you're doing in terms of vector maths.

    Secondly, you could just write
    Code:
    if (!forward[1] && !forward[0])
    instead of
    Code:
    if (forward[1] == 0 && forward[0] == 0)

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    14
    This function works with pointers and changes array elements outside its scope. In big programs this is not desired. Perhaps you should use classes and references (C++ style).

    Some people need faster sqrt-function than in <cmath>. Just try FastSQRT(...):
    Code:
    #include <iostream>
    #include <conio.h>
    #include <cmath>
    #include <ctime> // clock_t, clock(), ...
    
    using namespace std;
    
    float FastSQRT( float r )
    {
        // Null und negative Zahlen abfangen
        //if( r == 0.0f ) return 0.0f;
        if( r <  0.0f ) r = -r     ;
       
        // Startwert festlegen
        float val = r;
        float halfval = 0.5f * r;
        unsigned long *ptr = (unsigned long*)&r;
        *ptr = ( 0xbe6f0000 - *ptr ) >> 1;
    
        float temp = r;
    
        // Zwei Schritte reichen
        temp *= 1.5f - temp * temp * halfval;
        temp *= 1.5f - temp * temp * halfval;
        //temp *= 1.5f - temp * temp * halfval; //höhere Präzision   
       
        // Rückgabe
        return val*temp;
    }
    
    int main()
    {
      double x;
      cout << "Bitte Zahl eingeben fuer Wurzelziehen: ";
      cin  >> x;
      cout << FastSQRT(x) << endl;
      cout << sqrt(x)     << endl << endl;
        
      // Zeitmessung
      const int NMAX = 20;
      int max;
      cout << "Anzahl Schleifendurchlaeufe? ";
      cin >> max;
      clock_t t1,t2;
      double ts, tm=0;
    
      double a;
    
      cout << endl;
      for(int n=0; n<NMAX; ++n)
      {
          t1 = clock(); // Start
          for( int i=0; i<max;i++) 
          {
            a = FastSQRT(x); //zu messende Aktion 1
          } 
          t2 = clock(); // Ende
          ts = (t2-t1)/static_cast<float>(CLOCKS_PER_SEC); // Zeit in Sekunden.
          cout << "Zeit Aktion 1: " << ts << " sec" << endl;
          tm += ts; // Das ist das Gleiche wie tm = tm + ts       
      }
      tm/=NMAX; // Das ist das Gleiche wie tm = tm / NMAX
      cout << "Durchschnitts-Zeit bei Aktion 1: " << tm << " sec" << endl;
    
      tm=0;
      cout << endl;
      for(int n=0; n<NMAX; ++n)
      {
          t1 = clock();
          for( int i=0; i<max;i++) 
          {
            a = sqrt(x); //zu messende Aktion 2
          } 
          t2 = clock();
          ts = (t2-t1)/static_cast<float>(CLOCKS_PER_SEC);
          cout << "Zeit bei Aktion 2: " << ts << " sec" << endl;
          tm += ts;    
      }
      tm/=NMAX;
      cout << "Durchschnitts-Zeit bei Aktion 2: " << tm << " sec" << endl;
    
      getch();
    }
    Last edited by Erhard Henkes; 07-11-2004 at 08:20 AM.

  4. #4
    Registered User big146's Avatar
    Join Date
    Apr 2003
    Posts
    74
    Thks for the reply's. according to my output running the code u posted erhard, the sqrt functions is faster.
    big146

  5. #5
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    I think modern compilers use special intructions for sqrt(), making such hacks worthless.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  6. #6
    Registered User big146's Avatar
    Join Date
    Apr 2003
    Posts
    74
    What about using valarray for the code above?If yes, could someone show me how to implement it.
    big146

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    You should be able to create lookup tables for the atan2 calls.

    You might also want to read this site. I done too much with valarrays. http://cs.calvin.edu/books/c++/intro.../Valarrays.pdf

Popular pages Recent additions subscribe to a feed