Execution time bottleneck: How to speed up execution?

Doug Gwyn gwyn at smoke.brl.mil
Thu Feb 14 08:03:23 AEST 1991


In article <21658 at yunexus.YorkU.CA> racine at yunexus.yorku.ca (Jeff Racine) writes:
>   for (j=1; j <= n; j++) {
>      sum = 0.0;
>      for (i=1; i<= n; i++) {
>         sum += exp( con * (x[i]-x[j]) * (x[i]-x[j]) );
>      }
>      a[j] = k*sum;
>   }
>This part of the algorithmn is a bottleneck. I want to make this as
>quick as possible. What are my options? I have heard that assembler
>would be faster. How much faster would assembler be? Would it be worth
>the effort? Are there other alternatives that you can see?  Any
>improvement would be welcome.

Conversion to assembler would not help much, since the majority of
the time is spent in evaluating the exponential function.  There are
some simple rearrangements that would help substantially, however:
	(1)  Factor out the e^con and fold it into the "k" multiplier.
	(2)  If your compiler is not highly optimizing, introduce a
	register double variable into which x[i]-x[j] is stored, then
	mutliply that temporary by itself to produce the argument to
	exp().
	(3)  Consider precomputing the matrix exp((x[i]-x[j])^2), which
	is symmetric, so you need only compute a little more than half
	the [i,j] combinations.
If this is being used for some sort of matching procedure with integral
x[.] values, you might also consider using an information-statistics
procedure using n*log(n) terms instead; such terms can be precomputed
(or dynamically computed and cached) for a gain in efficiency.



More information about the Comp.lang.c mailing list