I had this problem and tried several stack samples to diagnose it. What this does is indicate where the calls are coming from and what the value of the argument is. I found that when calling exp from certain places, the value of the argument was strongly repeatable.
This suggested a memoization approach that made a huge difference.
He needs a simple wrapper function:
double exp_cached(double arg, double* old_arg, double* old_result){ if (arg== *old_arg) return *old_result; *old_arg = arg; *old_result = exp(arg); return *old_result; }
and wherever exp(foo) is called, do:
static double old_arg = -999999999, old_result; ... ... exp_cached(foo, &old_arg, &old_result)...
Thus, exp not called if its argument in the place where it is called has the same argument value as before.
Mike dunlavey
source share