B
Ben Bacarisse
I found your advice was really useful. Magic advice. Really
helpful. And sorry for giving a simplistic foo. But I did not mean
to fool you because in my opinion, the previous one shared some
similar patterns with the later one. You can disagree with me, but I
don't have to agree with you.
I don't understand this. All nine arguments were used in my foo
function body.
Yes, but not as passed, so why pass them? Here is the code:
static double foo(Vector *x, Vector *df,
double *f0, double *f1, double *f2,
double *g0, double *g1, double *g2,
unsigned long sz)
{
unsigned long i;
register double sum;
register double x0, x1, x2, y0, y1, y2;
register double z1, z2;
register double exp1, exp2;
f0 = para->prob0;
f1 = para->prob1;
f2 = para->prob2;
g0 = para->prob3;
g1 = para->prob4;
g2 = para->prob5;
You immediately assign to these parameters. This has no effect in the
calling function (if you intended it to, you have more work to do) and
any value calculated and passed is lost, so why pass them?
sum = 0.0;
df->data[0] = 0.0;
df->data[1] = 0.0;
exp1 = exp(x->data[0]);
exp2 = exp(x->data[1]);
for (i = 0; i < sz; i++) {
x0 = exp0*g0;
x1 = exp1*g1;
x2 = exp2*g2;
z1 = x0 + x1 + x2;
You could try:
z1 = exp0*g0 + x1 + x2;
here since x0 is not used later. Many compilers will have eliminated
the store for you already so you won't gain, but it is worth a try.
y0 = exp0*f0;
y1 = exp1*f1;
y2 = exp2*f2;
z2 = y0 + y1 + y2;
Same here.
sum += log(z1) - log(z2);
df->data[0] += x1/z1 - y1/z2;
df->data[1] += x2/z1 - y2/z2;
You've already done Eric's other suggestion (using locals for these
sums) so I won't repeat it.
}
return sum;
}