SUMMARY: Memory allocation / data access

James F. Blake jim at doctor.chem.yale.edu
Wed Mar 20 11:40:19 AEST 1991


  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 at bbn.com) 
James Davies (jrbd at craycos.com)
David Wald (wald at theory.lcs.mit.edu)
Harry Protoolis (harry at matilda.uk.sun.com)
Michael W. Balk (mwb at 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



More information about the Comp.lang.c mailing list