CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

What executes faster => pointers&funcs OR redundant o

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
weg22



Joined: 08 Jul 2005
Posts: 91

View user's profile Send private message

What executes faster => pointers&funcs OR redundant o
PostPosted: Fri Dec 15, 2006 9:58 am     Reply with quote

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;
}
Ttelmah
Guest







PostPosted: Fri Dec 15, 2006 3:30 pm     Reply with quote

The first will probably be fractionally faster, _but_ the difference will be tiny. The arithmetic itself, will take hundreds of times the time involved in calling the functions....

Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group