Advertisement

How slow is sinf()?

Started by July 13, 2003 05:21 PM
14 comments, last by theZapper 21 years, 7 months ago
When working out the sine/cosine of an angle (for camera rotation and movement), is it worth me creating a lookup table of entries and indexing into that, or is it much slower calling sinf()? I''m currently using a table with 720 entries but still get jerky movement, will it matter much if i just use the sine call instead? Thanks --- "When I''m in command, every mission''s a suicide mission" - Zapp Branigan
---When I'm in command, every mission's a suicide mission!
sinf is not that slow on modern hardware, you can call it several hundred times before it becomes noticeable, sqrt on the otherhand...
Advertisement
thanks, i''ll swap that in then.

And i''ll keep a wary eye out for the evil sqrt..!

---
"When I''m in command, every mission''s a suicide mission" - Zapp Branigan
---When I'm in command, every mission's a suicide mission!
quote:
Original post by theZapper
...is it worth me creating a lookup table of entries and indexing into that, or is it much slower calling sinf()? ...
I will answer that question with another question:

Is it worth hours of your time to optimizing some code that will make absolutely no detectable difference in the speed of your code?

BTW, same goes for sqrt().

[edited by - JohnBolton on July 13, 2003 9:25:27 PM]
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
I think sinf maps to a call to the sin routine in the math coprocessor, plus unnoticable overhead, most probably.

About sqrt(), I don''t think it''s going to be the bottleneck in your product/game.

Games and engines have more time consuming stuff than sqrt()''s (like AI, graphics, ...etc)

A final word: The profiler is your friend.

Peace,
Muhammad Haggag

Sin, cos and square-root can be made virtually free.
__asm{fld some_floatfsqrt               // or fsin, fcos, fpatan, f2xm1, whatever}// do some non-floating-point math while-u-wait__asm{fstp some_other_float}of course it only works if you have an opportunity to do non-math stuff before you need the result   


********


I am not the Iraqi information minister.

GSACP : GameDev Society Against Crap Posting
To join: Put these lines in your signature and don''t post crap!

spraff.net: don't laugh, I'm still just starting...
Advertisement
Walking Carcass - I do believe compiler writers are aware of that


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
not when sin is a funtion implemented in the language, the return value is used as soon as the function returns, which is before the return value exists (unless the function itself has ridiculous overhead) so the following code waits. some implementations might get it right, but for the effort it takes to write 3 assembly statements, it''s not worth the risk.

********


I am not the Iraqi information minister.

GSACP : GameDev Society Against Crap Posting
To join: Put these lines in your signature and don''t post crap!
spraff.net: don't laugh, I'm still just starting...
quote:
Original post by walkingcarcass
some implementations might get it right, but for the effort it takes to write 3 assembly statements, it''s not worth the risk.


I think it is, since those 3 assembly statements won''t compile on anything but x86 hardware. If you don''t KNOW it saves cycles that really needs to be saved and can''t be saved otherwise, assembly is a stupid place to go.
Maybe but the way I see it, those 3 statements are so incredibly easy to type it costs nothing in time or effort, if it shaves a few cycles off a math heavily loop it''s worth it. Assembly is not a stupid place to go if and only if it''s inappropiate. When it comes to sin, cos, sqrt etc assembly can be several times faster. fact.

********


I am not the Iraqi information minister.

GSACP : GameDev Society Against Crap Posting
To join: Put these lines in your signature and don''t post crap!
spraff.net: don't laugh, I'm still just starting...

This topic is closed to new replies.

Advertisement