The boolean logic in VMD does short circuit evaluation on an element-wise basis. For instance, given one atom, if X is true then X or Y will be true no matter the value of Y so there is no need to evaluate it. Similarly, if X is false, then X and Y will also be false so Y again need not be evaluated.
Knowing how short circuit selections work can speed up several
types of selections. Consider a system with a large number of waters
and a protein. The expression protein and segname < 10
is
faster than x < 10 and protein
since in the first selection
only the atoms which are proteins have the segname converted to
a number, while in the second selection, all the segment names are
converted.
The within selection has its own form of short circuiting. The command can be interpreted as ``find the atoms of A which are withing a given distance from B,'' and if A isn't given, search all the atoms. The search done in VMD takes a time roughly proportional to the number of atoms in A multiplied by the number of atoms in B, so reducing the number of atoms in A (ie, by not testing every atoms) make the search faster.
Using the system with a lot of water and a protein, compare
the selection protein within 5 of resid 1
to
(within 5 of resid 1) and protein
. The first is very fast as it
does a distance search between all the protein atoms and all the atoms
in resid 1. However, the second selection searches through all the
atoms for those which are within 5 Å of resid and then finds which
of those are protein atoms.