Calculating vs. lookup tables for sine value performance?

Viewed 19978

Let's say you had to calculate the sine (cosine or tangent - whatever) where the domain is between 0.01 and 360.01. (using C#)

What would be more performant?

  1. Using Math.Sin
  2. Using a lookup array with precalculated values

I would anticpate that given the domain, option 2 would be much faster. At what point in the precision of the domain (0.0000n) does the performance of the calculation exceed the lookup.

8 Answers

Sorry for grave digging, but there is a good solution for how to make quick indexing of lookup tables: https://jvm-gaming.org/t/fast-math-sin-cos-lookup-tables/36660

It's in Java, but it takes only a few minutes to port it to C#. I did tests and got the following results with 100000 iterations:

Math.Sin: 0.043 sec
Mathf.Sin: 0.06 sec (Unity`s Mathf lib)
MathTools.Sin: 0.026 (lookup tables static class).

Probably in Java it will give 50x boost (or it did in 2011 lol, but in C# in 2021 the difference is about 2x only).

Related