I used the chemkit library's Atom::isHeteroatom() method to perform the counting of the heteroatoms. For the timing I used Python's timeit module.
The code reads the 753 molecules from the MMFF validation suite's test file MMFF94_hypervalent.mol2 and then uses the timeit.Timer class to measure the execution time of counting the total number of heteroatoms.
Here is the code:
import timeit import chemkit # Counts the number of heteroatoms in the molecule # using an if statement def heteroatom_count_if(molecule): count = 0 for atom in molecule.atoms(): if atom.isHeteroatom(): count += 1 return count # Counts the number of heteroatoms in the molecule # using a try/except statement def heteroatom_count_try(molecule): count = 0 for atom in molecule.atoms(): try: assert(atom.isHeteroatom()) count += 1 except: continue return count # Counts the total number of heteroatoms in the list # of molecules using the given heteroatom_function def count(molecules, heteroatom_function): count = 0 for molecule in file.molecules(): count += heteroatom_function(molecule) return count if __name__ == '__main__': # read molecules from file file = chemkit.ChemicalFile("MMFF94_hypervalent.mol2") if not file.read(): print 'Error reading file: ' + file.errorString() exit() # list of molecules molecules = file.molecules() # measure heteroatom_count_if t = timeit.Timer("count(molecules, heteroatom_count_if)", "from __main__ import count, molecules, heteroatom_count_if") print 'heteroatom_count_if time: ' + str(t.timeit(500)) # measure heteroatom_count_try t = timeit.Timer("count(molecules, heteroatom_count_try)", "from __main__ import count, molecules, heteroatom_count_try") print 'heteroatom_count_try time: ' + str(t.timeit(500))And here are the results:
$ python heteroatoms.pySo we see that the if statement version is roughly twice as fast as the try/except version. That was my intuition, but it's good to be sure.
heteroatom_count_if time: 8.65832996368
heteroatom_count_try time: 17.5534369946