| weg22 
 
 
 Joined: 08 Jul 2005
 Posts: 91
 
 
 
			    
 
 | 
			
				| What executes faster => pointers&funcs OR redundant o |  
				|  Posted: Fri Dec 15, 2006 9:58 am |   |  
				| 
 |  
				| Hi all, 
 I'm trying to figure out what is more efficient.  I'm attempting many operations with a 4 component vector (quaternions for those who are familiar).  I'm trying to figure out if it's more efficient to pass the vector to a function (for example to multiply them) or compute the operation over several times in the code.  This also requires pointers since I'm returning an array.
 
 Or is the best way to figure this out to just look at the .LST files and see which one uses more ROM??
 
 For example:
 
 Code A (redundant ops)
 
  	  | Code: |  	  | // Calculate error quaternion => qe = (qc*)(qm)
 qe1 = qc4*qm1 + (-qc1)*qm4 + (-qc2)*qm3 - (-qc3)*qm2; // x
 qe2 = qc4*qm2 - (-qc1)*qm3 + (-qc2)*qm4 + (-qc3)*qm1; // y
 qe3 = qc4*qm3 + (-qc1)*qm2 - (-qc2)*qm1 + (-qc3)*qm4; // z
 qe4 = qc4*qm4 - (-qc1)*qm1 - (-qc2)*qm2 - (-qc3)*qm3; // w
 // normalize
 norm = sqrt(qe1*qe1 + qe2*qe2 + qe3*qe3 + qe4*qe4);
 qe1 = qe1/norm;
 qe2 = qe2/norm;
 qe3 = qe3/norm;
 qe4 = qe4/norm;
 
 | 
 
 Code B (functions and pointers)
 
  	  | Code: |  	  | main{
 float *qe
 
 // Calculate error quaternion and normalize => qe = (qc*)(qm)
 qe = multiplyQ(-qc1, -qc2, -qc3, qc4, qm1, qm2, qm3, qm4);
 qe1 = qe[0]; qe2 = qe[1]; qe3 = qe[2]; qe4 = qe[3];
 }
 
 float *multiplyQ(float q1_x, float q1_y, float q1_z, float q1_w, float q2_x, float q2_y, float q2_z, float q2_w)
 {
 float norm;
 
 // initialize
 qr[0] = 0.0; qr[1] = 0.0; qr[2] = 0.0; qr[3] = 0.0;
 
 // compute resultant quaternion
 qr[0] = q1_w*q2_x + q1_x*q2_w + q1_y*q2_z - q1_z*q2_y; // x
 qr[1] = q1_w*q2_y - q1_x*q2_z + q1_y*q2_w + q1_z*q2_x; // y
 qr[2] = q1_w*q2_z + q1_x*q2_y - q1_y*q2_x + q1_z*q2_w; // z
 qr[3] = q1_w*q2_w - q1_x*q2_x - q1_y*q2_y - q1_z*q2_z; // w
 
 // normalize
 norm = sqrt(qr[0]*qr[0] + qr[1]*qr[1] + qr[2]*qr[2] + qr[3]*qr[3]);
 
 // compute resultant quaternion
 qr[0] = qr[0]/norm; // x
 qr[1] = qr[1]/norm; // y
 qr[2] = qr[2]/norm; // z
 qr[3] = qr[3]/norm; // w
 
 return qr;
 }
 
 | 
 |  |