I am using sin() and cos() built in functions in my C++ code. However after profiling it I realized that the 2 functions consume a lot of time. So I decided to eliminate sin() and cos() functions and replace it with an equivalent code that generates the same values for me, using either trigonometric identities or something else (Please suggest!). I tried an approach, which is shown below, but I am not getting the required results due to errors in it (The problem I assume is because of the large number of loops it goes through -- 35000*35000 which marginally moves it away from the desired result). Can somebody help me with this or another approach which might be more precise and correct. I have not shown the entire code because its big and unwanted. I just want a replacement for sin() and cos().
Code:
// This is the code that works correctly but consumes a lot of time
main()
float phase=0.0, rate=2.5e6, raw[35000],multcos[35000],multsin[35000];
{
//some statements here
for(int i=0;i<35000;i++)
{
//some statements here
for(int j=0;j<35000;j++)
{
arg=(2*3.14*fc*j)/rate + phase;
multcos[j]=raw[j]*cos(arg); //I want to replace this cos()
multsin[j]=raw[j]*sin(arg); //I want to replace this sin()
//raw[j] has some predefined values
Sum1[H[j]]=Sum1[H[j]] + multcos[j];
Sum2[H[j]]=Sum2[H[j]] + multsin[j];
//H[j] has some predefined values;
//Sum1[] has been initilized to 0;
}
//some statements here
}
//some statements here
}//end of main
Below is the replacement/equivalent code I wrote to replace sin and cos functions.
Code:
//This is the equivalent code that does not work correctly
float G_cb, G_b=1.57071406, G_a=0.0;
float G_snm2, G_snm1, G_cnm2, G_cnm1;
float G_s,G_c;
for(i=0;i<35000;i++)
{
G_cb = 2* cos(G_b * PI/180);
G_snm2 = sin(G_a + G_b); G_snm1 = sin(G_a + 2*G_b);
G_cnm2 = cos(G_a + G_b); G_cnm1 = cos(G_a + 2*G_b);
for(int j=0;j<35000;j++)
{
G_s = (G_cb * G_snm1) - G_snm2 ;
G_c = (G_cb * G_cnm1) - G_cnm2 ;
G_snm2 = G_snm1; G_cnm2 = G_cnm1;
G_snm1 = G_s; G_cnm1 = G_c;
multcos[j]= raw[j] * G_c;
multsin[j]= raw[j] * G_s;
Sum1[H[j]]=Sum1[H[j]] + multcos[j];
Sum2[H[j]]=Sum2[H[j]] + multsin[j];
//H[j] has some predefined values;
//Sum1[] has been initilized to 0;
}
}
Thanks,
prads

