• ### Announcements

#### Archived

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

# cos/sin or sqrt() ?

## 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 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 on other sites
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 on other sites
I know that with gcc you can select the level of optimization. Is there a way you could select no optimization?

"Donkey, if it were me, you''d be dead."
I cna ytpe 300 wrods pre mniute.

##### Share on other sites
By the way, can you measure the same thing using assembly intrinsics, possibly using SSE?

##### 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 on other sites
in the real world the algorithms you use and cache misses dictate performance. Not the individual functions called.

##### 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.

• ### Forum Statistics

• Total Topics
627711
• Total Posts
2978744

• 21
• 14
• 12
• 36
• 30