When profiling code that utilized the reader functions, it was noticed that the 'nodes_of_elems' function took a disproportionate amount of time on a large mili database. I don't claim to be an ace Python developer but I made some modifications to a few lines that took the runtime down by two orders of magnitude. The function in question is shown below, with the original lines commented out and my changes noted.
def nodes_of_elems( self, class_sname, elem_labels ):
''' Find nodes associated with elements by label '''
if type(elem_labels) is not list:
if iterable(elem_labels):
elem_labels = list(elem_labels)
else:
elem_labels = [ elem_labels ]
if class_sname not in self.__class_to_sclass:
return np.empty([1,0],dtype=np.int32)
if class_sname not in self.__conns:
return np.empty([1,0],dtype=np.int32)
# Original code
#if any( label not in self.__labels[class_sname] for label in elem_labels ):
# return np.empty([1,0],dtype=np.int32)
# Changes
if not all(np.isin(elem_labels, self.__labels[class_sname] )):
return np.empty([1,0],dtype=np.int32)
# get the indices of the labels we're querying in the list of local labels of the element class, so we can retrieve their connectivity
# Original code
#indices = (self.__labels[class_sname][:,None] == elem_labels).argmax(axis=0)
#elem_conn = self.__conns[class_sname][indices][:,:-1]
# Changes
indices=np.searchsorted(elem_labels,self.__labels[class_sname][:,None])
elem_conn = self.__conns[class_sname][indices][:,-1][:,0:-1]
return self.__labels['node'][elem_conn], self.__labels[class_sname][indices,None]
When profiling code that utilized the reader functions, it was noticed that the 'nodes_of_elems' function took a disproportionate amount of time on a large mili database. I don't claim to be an ace Python developer but I made some modifications to a few lines that took the runtime down by two orders of magnitude. The function in question is shown below, with the original lines commented out and my changes noted.