cos/sin or sqrt() ?
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...
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]
March 18, 2004 08:00 PM
Won''t the compiler optimise out the loop? Or maybe the processor will cache the previous result from the last iteration round the loop?
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.
"Donkey, if it were me, you''d be dead."
I cna ytpe 300 wrods pre mniute.
"Donkey, if it were me, you'd be dead."I cna ytpe 300 wrods pre mniute.
By the way, can you measure the same thing using assembly intrinsics, possibly using SSE?
Previously "Krohm"
quote: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.
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...
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?
in the real world the algorithms you use and cache misses dictate performance. Not the individual functions called.
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.
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.

If at first you don't succeed, redefine success.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement