Precomputed Cos, Sin [Solved]

Started by
5 comments, last by Sneftel 15 years, 10 months ago
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]

[Hardware:] Falcon Northwest Tiki, Windows 7, Nvidia Geforce GTX 970

[Websites:] Development Blog | LinkedIn
[Unity3D :] Alloy Physical Shader Framework

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.
Have you profiled? Is sin/cos your bottleneck? Don't prematurely optimise.
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
Okay, thanks for the info.

[Hardware:] Falcon Northwest Tiki, Windows 7, Nvidia Geforce GTX 970

[Websites:] Development Blog | LinkedIn
[Unity3D :] Alloy Physical Shader Framework

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

This topic is closed to new replies.

Advertisement