Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 547153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:56:34+00:00 2026-05-13T10:56:34+00:00

I have the following code doing Sin/Cos function using a pre-calculated memory table. in

  • 0

I have the following code doing Sin/Cos function using a pre-calculated memory table. in the following example the table has 1024*128 items covering all the Sin/Cos values from 0 to 2pi. I know I can use Sin/Cos symmetry and hold only 1/4 of the values but them I will have more ‘ifs’ when computing the value.

private const double PI2 = Math.PI * 2.0; 
private const int TABLE_SIZE = 1024 * 128;
private const double TABLE_SIZE_D = (double)TABLE_SIZE;
private const double FACTOR = TABLE_SIZE_D / PI2;

private static double[] _CosineDoubleTable;
private static double[] _SineDoubleTable;

Set the translation table

private static void InitializeTrigonometricTables(){
   _CosineDoubleTable = new double[TABLE_SIZE];
   _SineDoubleTable = new double[TABLE_SIZE];

   for (int i = 0; i < TABLE_SIZE; i++){
      double Angle = ((double)i / TABLE_SIZE_D) * PI2;
      _SineDoubleTable[i] = Math.Sin(Angle);
      _CosineDoubleTable[i] = Math.Cos(Angle);
   }
}

The Value is a double in radians.

Value %= PI2;  // In case that the angle is larger than 2pi
if (Value < 0) Value += PI2; // in case that the angle is negative
int index = (int)(Value * FACTOR); //from radians to index and casted in to an int
double sineValue = _SineDoubleTable[index]; // get the value from the table

I’m looking for a faster way to do this. The above 4 lines are ~25% of the whole process (executed billions of times).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T10:56:34+00:00Added an answer on May 13, 2026 at 10:56 am

    You could try to use unsafe code to eliminate array bounds checking.
    But even a unsafe, optimized version does not seem to come anywhere near Math.Sin.

    Results based on 1’000’000’000 iterations with random values:

    (1) 00:00:57.3382769  // original version
    (2) 00:00:31.9445928  // optimized version
    (3) 00:00:21.3566399  // Math.Sin
    

    Code:

    static double SinOriginal(double Value)
    {
        Value %= PI2;
        if (Value < 0) Value += PI2;
        int index = (int)(Value * FACTOR);
        return _SineDoubleTable[index];
    }
    
    static unsafe double SinOptimized(double* SineDoubleTable, double Value)
    {
        int index = (int)(Value * FACTOR) % TABLE_SIZE;
        return (index < 0) ? SineDoubleTable[index + TABLE_SIZE]
                           : SineDoubleTable[index];
    }
    

    Test program:

    InitializeTrigonometricTables();
    Random random = new Random();
    
    SinOriginal(random.NextDouble());
    var sw = System.Diagnostics.Stopwatch.StartNew();
    for (long i = 0; i < 1000000000L; i++)
    {
        SinOriginal(random.NextDouble());
    }
    sw.Stop();
    Console.WriteLine("(1) {0}  // original version", sw.Elapsed);
    
    fixed (double* SineDoubleTable = _SineDoubleTable)
    {
        SinOptimized(SineDoubleTable, random.NextDouble());
        sw = System.Diagnostics.Stopwatch.StartNew();
        for (long i = 0; i < 1000000000L; i++)
        {
            SinOptimized(SineDoubleTable, random.NextDouble());
        }
        sw.Stop();
        Console.WriteLine("(2) {0}  // optimized version", sw.Elapsed);
    }
    
    Math.Sin(random.NextDouble());
    sw = System.Diagnostics.Stopwatch.StartNew();
    for (long i = 0; i < 1000000000L; i++)
    {
        Math.Sin(random.NextDouble());
    }
    sw.Stop();
    Console.WriteLine("(3) {0}  // Math.Sin", sw.Elapsed);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 458k
  • Answers 458k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you use pattern matching in your for-comprehension the compiler… May 15, 2026 at 11:11 pm
  • Editorial Team
    Editorial Team added an answer Scenarios in which you might want to keep them separate:… May 15, 2026 at 11:11 pm
  • Editorial Team
    Editorial Team added an answer From PHP Manual on Callbacks: A method of an instantiated… May 15, 2026 at 11:10 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.