Hi MDAnalysis!
I'm writing a custom hydrogen bonding analysis code and need to loop a lot over atoms. Right now my code is doing this:
for atom in atomGroup:
pos1 = atom.position
for atom2 in atomGroup2:
pos2 = atom2.position
doStuffWithPositions(pos1, pos2)
I tried looping over indices, like this
for i in range(atomGroup.n_atoms):
pos1 = atomGroup.positions[i]
for j in range(atomGroup2.n_atoms):
pos2 = atomGroup2.positions[j]
doStuffWithPositions(pos1, pos2)
but it was (even) slower. I noticed with the first version that just looping over the atoms took up 1/3 of the total time in my case (doStuffWithPositions etc are optimized Cython routines)! How can I loop over atoms faster? Why is there so much overhead? I need to extract positions, ix, and other information from each hydrogen bond so I can't just loop over position arrays. I am not doing any select_atoms() inside the loop of course.
Thank you!
Dan