R
razor
Dear all,
I'm struggling with a C program (since I'm new to C...) therefore, I
count on you to help me a hand speeding up this piece, since it's the
bottleneck of the program.
I should give you some extra information
- the arrays m , ha, ad is 1e3 to 1e4 elements long, dependent on the
input. The code I show you below is part of a for loop, looping over
all elements i.
- none of the cases in the switch case construction is likely to occur
more often than the other ones.
- I used cosalfa and sinalfa variables, because I need these values
later in the program, and calculating cos and sin is expensive I
guess ...
- this program runs for a very large numbers of times, so a small
speedup is worth it...
Here's the code:
yp = sqrt(Ri*Ri-(xmo-xmi)*(xmo-xmi))+ymi;
switch((yi>yp) + 2*(yi<ymi)) {
case 1:{
alfa = 0;
cosalfa = 1;
sinalfa = 0;
c = 0;
x = xi;
y = yi;
G = 0;
r = ((x-xi)*(x-xi)+(y-yi)*(y-yi));
if (r<(ha*ha)){
H = ad;}
else {H = 0;}
break;}
case 2:{
alfa = 0;
cosalfa = 1;
sinalfa = 0;
c = 1;
x = xi;
y = yp;
ti = (vi*cosalfa+vfi*sinalfa);
G = -m*sgn(ti);
H = 0;
break;}
default:{
gamma = atan2((yi-ymi),(xmo-xmi));
x = xmi+Ri*cos(gamma);
y = ymi+Ri*sin(gamma);
alfa = atan2((x-xmo),(y-yi));
cosalfa = cos(alfa);
sinalfa = sin(alfa);
x = x+m;
c = 1;
ti = (vi*cosalfa+vfi*sinalfa);
G = -m*sgn(ti);
H = 0;
break;}
}
Thanks in advance,
Kizzie
I'm struggling with a C program (since I'm new to C...) therefore, I
count on you to help me a hand speeding up this piece, since it's the
bottleneck of the program.
I should give you some extra information
- the arrays m , ha, ad is 1e3 to 1e4 elements long, dependent on the
input. The code I show you below is part of a for loop, looping over
all elements i.
- none of the cases in the switch case construction is likely to occur
more often than the other ones.
- I used cosalfa and sinalfa variables, because I need these values
later in the program, and calculating cos and sin is expensive I
guess ...
- this program runs for a very large numbers of times, so a small
speedup is worth it...
Here's the code:
yp = sqrt(Ri*Ri-(xmo-xmi)*(xmo-xmi))+ymi;
switch((yi>yp) + 2*(yi<ymi)) {
case 1:{
alfa = 0;
cosalfa = 1;
sinalfa = 0;
c = 0;
x = xi;
y = yi;
G = 0;
r = ((x-xi)*(x-xi)+(y-yi)*(y-yi));
if (r<(ha*ha)){
H = ad;}
else {H = 0;}
break;}
case 2:{
alfa = 0;
cosalfa = 1;
sinalfa = 0;
c = 1;
x = xi;
y = yp;
ti = (vi*cosalfa+vfi*sinalfa);
G = -m*sgn(ti);
H = 0;
break;}
default:{
gamma = atan2((yi-ymi),(xmo-xmi));
x = xmi+Ri*cos(gamma);
y = ymi+Ri*sin(gamma);
alfa = atan2((x-xmo),(y-yi));
cosalfa = cos(alfa);
sinalfa = sin(alfa);
x = x+m;
c = 1;
ti = (vi*cosalfa+vfi*sinalfa);
G = -m*sgn(ti);
H = 0;
break;}
}
Thanks in advance,
Kizzie