loopsbest of ms per loop this seemingly innocuous example illustrates that it is worth understanding the performance characteristics of the python standard librarynumpypandasand other libraries used in this book in larger-scale data analysis applicationsthose milliseconds will start to add up%timeit is especially useful for analyzing statements and functions with very short execution timeseven at the level of microseconds ( - secondsor nanoseconds ( - secondsthese may seem like insignificant amounts of timebut of course microsecond function invoked million times takes seconds longer than microsecond function in the above examplewe could very directly compare the two string operations to understand their performance characteristicsin [ ] 'foobarin [ ] 'fooin [ ]%timeit startswith( loopsbest of ns per loop in [ ]%timeit [: = loopsbest of ns per loop basic profiling%prun and %run - profiling code is closely related to timing codeexcept it is concerned with determining where time is spent the main python profiling tool is the cprofile modulewhich is not specific to ipython at all cprofile executes program or any arbitrary block of code while keeping track of how much time is spent in each function common way to use cprofile is on the command linerunning an entire program and outputting the aggregated time per function suppose we had simple script which does some linear algebra in loop (computing the maximum absolute eigenvalues of series of matrices)import numpy as np from numpy linalg import eigvals def run_experiment(niter= ) results [for in xrange(niter)mat np random randn(kkmax_eigenvalue np abs(eigvals(mat)max(results append(max_eigenvaluereturn results some_results run_experiment(print 'largest one we saw%snp max(some_results ipythonan interactive computing and development environment www it-ebooks info