for larger-scale or longer-running data analysis applicationsyou may wish to measure the execution time of various components or of individual statements or function calls you may want report of which functions are taking up the most time in complex process fortunatelyipython enables you to get this information very easily while you are developing and testing your code timing code by hand using the built-in time module and its functions time clock and time time is often tedious and repetitiveas you must write the same uninteresting boilerplate codeimport time start time time(for in range(iterations)some code to run here elapsed_per (time time(startiterations since this is such common operationipython has two magic functions %time and %timeit to automate this process for you %time runs statement oncereporting the total execution time suppose we had large list of strings and we wanted to compare different methods of selecting all strings starting with particular prefix here is simple list of , strings and two identical methods of selecting only the ones that start with 'foo' very large list of strings strings ['foo''foobar''baz''qux''python''guido van rossum' method [ for in strings if startswith('foo')method [ for in strings if [: ='foo'it looks like they should be about the same performance-wiserightwe can check for sure using %timein [ ]%time method [ for in strings if startswith('foo')cpu timesuser ssys stotal wall time in [ ]%time method [ for in strings if [: ='foo'cpu timesuser ssys stotal wall time the wall time is the main number of interest soit looks like the first method takes more than twice as longbut it' not very precise measurement if you try %time-ing those statements multiple times yourselfyou'll find that the results are somewhat variable to get more precise measurementuse the %timeit magic function given an arbitrary statementit has heuristic to run statement multiple times to produce fairly accurate average runtime in [ ]%timeit [ for in strings if startswith('foo') loopsbest of ms per loop software development tools www it-ebooks info