How can I optimize vector normalization in a Molecular Modeling program?

Carsten Hansen hansen.c at worldnet.att.net
Mon Nov 8 16:11:50 EST 1999

```One thing you could try is to multiply by the reciprocal

VTYPE rec;

rec = 1.0 / length;

v[0] *= rec;
v[1] *= rec;
v[2] *= rec;

I would be interested in knowing what difference that makes.

Carsten Hansen

----------
In article <807bh2\$h4k\$1 at nnrp1.deja.com>, willkn at montana.com wrote:

> Hi, I've been working on a molecular modeling program and have been
> trying to improve its performance recently.  I did some profiling and
> found that the most time by far (41.4%) was spent in a function which
> normalizes a 3-dimensional vector (profiling statistics are given at
> the bottom).
>
> I am wondering if anyone has suggestions for optimizing that operation.
> Thanks,
> William Knight
> willkn at montana.com
>
> The definition of the function is given below:
>
> typedef double VTYPE;
> typedef VTYPE Vector[3];
>
>
> ////////////////////////////////////////////////////////////////////////
> ////////
> // MakeUnitVector
> //
> // Reduces a normal vector specified as a set of three coordinates,
> // to a unit normal vector of length one.
> //
> bool MakeUnitVector(Vector v)
>  {
>  VTYPE length;
>
>  // Calculate the length of the vector
>  length = (VTYPE)sqrt((v[0]*v[0]) + (v[1]*v[1]) + (v[2]*v[2]));
>
>  // if length is zero, bail
>  if(length == 0)
>   return false;
>
>  // Divide each element by the length to get a unit vector.
>  v[0] /= length;
>  v[1] /= length;
>  v[2] /= length;
>
>  return true;
>  }// MakeUnitVector
>
> //***************** Profiling Details ************//
>
>         Func          Func+Child           Hit
>         Time   %         Time      %      Count  Function
> ---------------------------------------------------------
>    15493.876  41.4    15493.876  41.4  9971880 MakeUnitVector(double *
> const) (vector.obj)
>     5951.749  15.9     6789.747  18.1   627030
> CForceField::CalcVanderwaalsInteraction(void) (cforcefield.obj)
>     2185.047   5.8    18425.737  49.2    84010
> CForceField::CalcPotentialsAndForces(class CFFPairList *,class
> CEEPairList *,class CEOPairList *) (cforcefield.obj)
>     1976.562   5.3    13039.435  34.8  3752280
> CForceField::CalcEEInteraction(class CElectron *,class CElectron *)
> (cforcefield.obj)
>     1778.265   4.7     1778.265   4.7  4656515 VectorMagnitude(double *
> const) (vector.obj)
>     1668.910   4.5     1668.910   4.5  8884682 MultiplyVector(double *
> const,double) (vector.obj)
>     1270.428   3.4     1270.428   3.4 13438760 SubtractVector(double *
> const,double * const) (vector.obj)
>     1195.278   3.2     1195.278   3.2 21288242 AddVector(double *
> const,double * const) (vector.obj)
>     1145.663   3.1     1238.959   3.3       64 CStatus::Print(char *)
> (cstatus.obj)
>
>
>
> Sent via Deja.com http://www.deja.com/
> Before you buy.

```