[comp.lang.c] SUMMARY: Memory allocation / data access

jim@doctor.chem.yale.edu (James F. Blake) (03/20/91)

  Earlier I inquired about allocating memory and the efficiency of various 
addressing schemes.  I would like to thank the following individuals for
their helpful comments:

Rich Salz (rsalz@bbn.com) 
James Davies (jrbd@craycos.com)
David Wald (wald@theory.lcs.mit.edu)
Harry Protoolis (harry@matilda.uk.sun.com)
Michael W. Balk (mwb@ulysses.att.com)

The general consensus is that:

for (k = 0; k < natoms; k++) {
  for (l = 0; l < natoms; l++) {
    xdis = fabs (monomer[i].atom[k].x - monomer[j].atom[l].x);
    ydis = fabs (monomer[i].atom[k].y - monomer[j].atom[l].y);
    zdis = fabs (monomer[i].atom[k].z - monomer[j].atom[l].z);

should be accessed as:

atom_a = monomer[i].atom;
for (k = 0; k < natoms; k++, atom_a++) {
  for (atom_b = monomer[j].atom, l = 0; l < natoms; l++, atom_b++) {
    xdis = fabs (atom_a->x - atom_b->x);
    ydis = fabs (atom_a->y - atom_b->y);
    zdis = fabs (atom_a->z - atom_b->z);

This give ca. 7-10% speed-up on our SGI Iris 4D's.

Also, Rich Salz provided the following useful macro:

#define NEW(T, c)     ((T *)calloc((unsigned int) c, sizeof (T)))

if ((monomer = NEW(solvent, nmol)) == NULL) ...

Thanks once again for the advice.

  Jim