Often, when we are working in the Python REPL, we would like to be able to quickly and easily benchmark a line of code or a function. IPython makes this possible through a magic function called timeit
.
Perform the following set of steps to try out %timeit
to profile the code:
asa.py
source code.ipython
In [1]: import math In [2]: %timeit math.sqrt(10000)
10000000 loops, best of 3: 166 ns per loop
%timeit
, due to the rapid execution of the math.sqrt()
method, tested the function execution 10,000,000 times to get a more accurate measurement.%timeit
to test the main loop of the asa
code calculation. For this, we must first import the relevant functions:In [2]: from asa import * In [3]: import math In [4]: from vector3d import pos_distance, Vector3d, pos_distance_sq In [5]: import molecule
calculate_asa
function:In [13]: mol = molecule.Molecule('1R0R.pdb') In [14]: atoms = mol.atoms() In [15]: molecule.add_radii(atoms) In [16]: n_sphere = 960
%timeit
command to profile the function in question:In [18]: %timeit asas = calculate_asa(atoms, 1.4, n_sphere)
time
command:1 loops, best of 3: 52.5 s per loop
Notice that there appears to be a bit of an overhead added to the execution time using %timeit
.
The %timeit
magic function is a bit smarter than the previously mentioned profiling tools in that it will run the specified code multiple times to get a better estimate of the code's true execution time. If the code takes longer to run, it will reduce the number of repetitions performed for benchmarking.
13.59.177.14