开发者

How expensive is fmod in terms of processor time?

In my game开发者_C百科, I need to ensure that angles do not exceed 2 pi. so I use fmod(angle,TWO_PI);

Is this noticeably expensive to do about 100 times per second?


100 times per second? That's almost zero, you shouldn't trouble yourself.

Even if fmod takes 100 clock cycles - that's 10,000 cycles/Sec. For 1 1GHz CPU - that's 0.001% CPU.

BTW: why do you want to do fmod of TWO_PI? If you're going to take sin() or cos() - you can skip it.


If you want to ensure that angles do not exceed 2pi radians, you should use angle < TWO_PI. Using fmod will give you the remainder, which is useful if you want to find the actual angle and ignore multiple revolutions, but doesn't give you any information about which is greater.

Using < is very efficient, and as long as you aren't doing it 100,000+ times a second or don't have a lot of other code involved in the loop you should be fine. fmod is a fair bit more expensive as it involves division AND floating-point arithmetic, but 100 times per second is still almost negligible on most modern hardware, so I doubt you'll have much trouble at all. If you're still worried, do some tests. If you need help interpreting the tests or have other specific questions, post the code and we'll help you analyze them. :D

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜