Path A - Module 49: Performance Optimization#

Speed is a feature. When you are analyzing a microsecond-scale trajectory of an Alzheimer’s fibril, every millisecond counts.

MolSysMT ships its heavy mathematical kernels as a precompiled Rust extension. In this module, you will learn to separate import, preparation, and steady-state native execution costs.

import molsysmt as msm
import time
from molsysmt import systems

1. Separating import and execution costs#

The native kernels require no first-use compilation. A first call can still include lazy Python imports, data validation, and unit conversion, so benchmarks should report setup and steady-state execution separately.

lysozyme = systems['T4 lysozyme L99A']['181l.h5msm']

# First call (lazy imports, preparation, and execution)
start = time.time()
msm.structure.get_distances(lysozyme, selection='atom_index==[0,1,2]', selection_2='atom_index==[10,11,12]')
end = time.time()
print(f"First call time: {end - start:.4f} seconds")

# Repeated call (preparation and execution)
start = time.time()
msm.structure.get_distances(lysozyme, selection='atom_index==[0,1,2]', selection_2='atom_index==[10,11,12]')
end = time.time()
print(f"Second call time: {end - start:.4f} seconds")

2. Benchmark the operation you actually use#

Native kernels are already compiled. Measure realistic selections, structure counts, periodic conditions, and output types instead of timing an artificial kernel warm-up.

# Record enough context to reproduce the benchmark.
print(f"MolSysMT version: {msm.__version__}")
print("Native kernels are precompiled; no warm-up call is required.")

3. Benchmark checklist#

  • Report the MolSysMT version, hardware, system size, and number of structures.

  • Separate loading and validation from repeated numerical execution.

  • Verify scientific equality before comparing performance.


🏆 Path A Challenge: The Speed Demon#

  1. Restart your Python kernel to obtain a clean import state.

  2. Measure the first-call and repeated-call time for RMSD on a small trajectory.

  3. Repeat the measurement with a larger structure selection and explain the scaling.

  4. Report loading/preparation cost separately from steady-state native execution.

Your pipeline is now optimized for speed! In Module 50, we will learn how to work entirely in memory without ever creating files on disk: Virtual Forms & Memory I/O.