Archived

This topic is now archived and is closed to further replies.

cos/sin or sqrt() ?

This topic is 5017 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Any idea which is more computationally more expensive...performing a cos and a sin calculation (both) or a square root (just the one). I know sqrt is quite expensive...

Share this post


Link to post
Share on other sites

unsigned long time = timeGetTime();
for(int i=0; i<100000; i++)
{
cos(200); sin(200);
}

cout << "Cos/Sin time:" << timeGetTime()-time << endl;

time = timeGetTime();
for(i=0; i<100000; i++)
{
sqrt(200);
}

cout << "sqrt time:" << timeGetTime()-time << endl;


Have you tried something like that ?

EDIT: just tried it for the fun, cos/sin take between 12 and 13 milliseconds and sqrt take only 2 to 3 milliseconds

Edit2: With bigger number ( I tried 1000000000 ), it 15 ms for cos/sin and doesn't change with sqrt
And with a big floating points number ( 123123123.123123123f ), it takes the same time ( 15ms for cos/sin and 2-3ms for sqrt )

[edited by - Hedos on March 18, 2004 7:52:11 PM]

[edited by - Hedos on March 18, 2004 7:55:36 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Won''t the compiler optimise out the loop? Or maybe the processor will cache the previous result from the last iteration round the loop?

Share this post


Link to post
Share on other sites
quote:
Original post by alex997
Any idea which is more computationally more expensive...performing a cos and a sin calculation (both) or a square root (just the one). I know sqrt is quite expensive...
Expensive is a relative term. If you''re a millionaire, a $33,000 Toyota Camry is not expensive; if you make $10/hr, it is.

Have you completed the application? Have you profiled? Do you know how frequently this branch is being taken, and if it degrades performance?

Or is this just an abstract "theoretical" question?

Share this post


Link to post
Share on other sites
Profiling to the code using rtdsc I got:

Sin + Cos : An average of 427 cycles.
1 Sqrt : 200 cycles.

Of course, this will vary from CPU to CPU, and is quite useless information, but then, the question is just as pointless.

You have to remember that you''re unique, just like everybody else.

Share this post


Link to post
Share on other sites