Jump to content
  • Advertisement
Sign in to follow this  
n00body

Precomputed Cos, Sin [Solved]

This topic is 3665 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

Just trying to find more information on a trick I heard about a while back. Basically, computing arrays of cos() and sin() values, where the index is 0-360, to avoid the costly recomputation every time. The end result having negligible visual differences from the original, and running significantly faster, at the cost of extra storage for the arrays of values. Is this trick valid? If so, can anyone who has used it give me some idea of the costs vs benefits in real world usage? Thanks. :) [Edited by - n00body on June 11, 2008 1:42:21 PM]

Share this post


Link to post
Share on other sites
Advertisement
This trick was valid but the risk to get a cache miss today is higher than to get any benefit from this aproach.

In the past the calls to sin/cos where relative expansive.

But today I wouldn't bother until you may find out that this is a bottleneck in your app.

Share this post


Link to post
Share on other sites
This hasn't been an issue except in very specific cases since the 90s or so. Your information is out of date.

Never optimize like this unless you've run a profiler and found decisively that sin/cos are a problem for your application

-me

Share this post


Link to post
Share on other sites
It depends on your target language but in general i would say that is correct. i'll give an example:

AS3:
pre-computed array of sin from 0-360 degrees
av. exec time with random input: 81.4ns

computed on the fly
ev. exec time with random input: 157ns

so about twice as fast:

C++:
pre-computed array of sin from 0-360 degrees
av. exec time with random input: 9.4ns

computed on the fly
ev. exec time with random input: 125ns

so about 13x as fast.

But as has been said, it really isn't necessary unless this is actually serving as your bottleneck.

Share this post


Link to post
Share on other sites
Quote:
Original post by luca-deltodesco
It depends on your target language but in general i would say that is correct. i'll give an example:

AS3:
pre-computed array of sin from 0-360 degrees
av. exec time with random input: 81.4ns

computed on the fly
ev. exec time with random input: 157ns

so about twice as fast:

C++:
pre-computed array of sin from 0-360 degrees
av. exec time with random input: 9.4ns

computed on the fly
ev. exec time with random input: 125ns

so about 13x as fast.

And these benchmarks are in the context of an actual game, then? Which is doing things other than calculating sines and cosines in an inner loop? If not, you need to understand and internalize the concept of cache coherence.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!