î e fÄ.ã@sÿdZddlZddlZddlZddlZddddgZdZdZd Zej Z d Z d d „Z d d„Z Gdd„dƒZdde edd„Zdde eedd„Zddddd„ZedkrûejeƒƒndS)a2Tool for measuring execution time of small code snippets. This module avoids a number of common traps for measuring execution times. See also Tim Peters' introduction to the Algorithms chapter in the Python Cookbook, published by O'Reilly. Library usage: see the Timer class. Command line usage: python timeit.py [-n N] [-r N] [-s S] [-t] [-c] [-p] [-h] [--] [statement] Options: -n/--number N: how many times to execute 'statement' (default: see below) -r/--repeat N: how many times to repeat the timer (default 3) -s/--setup S: statement to be executed once initially (default 'pass'). Execution time of this setup statement is NOT timed. -p/--process: use time.process_time() (default is time.perf_counter()) -t/--time: use time.time() (deprecated) -c/--clock: use time.clock() (deprecated) -v/--verbose: print raw timing results; repeat for more digits precision -h/--help: print this usage message and exit --: separate options from statement, use when statement starts with - statement: statement to be timed (default 'pass') A multi-line statement may be given by specifying each line as a separate argument; indented lines are possible by enclosing an argument in quotes and using leading spaces. Multiple -s options are treated similarly. If -n is not given, a suitable number of loops is calculated by trying successive powers of 10 until the total time is at least 0.2 seconds. Note: there is a certain baseline overhead associated with executing a pass statement. It differs between versions. The code here doesn't try to hide it, but you should be aware of it. The baseline overhead can be measured by invoking the program without arguments. Classes: Timer Functions: timeit(string, string) -> float repeat(string, string) -> list default_timer() -> float éNÚTimerÚtimeitÚrepeatÚ default_timerz i@Bézˆ def inner(_it, _timer{init}): {setup} _t0 = _timer() for _i in _it: {stmt} _t1 = _timer() return _t1 - _t0 cCs|jddd|ƒS)z*Helper to reindent a multi-line statement.Ú ú )Úreplace)ÚsrcÚindent©r ú+/opt/alt/python34/lib64/python3.4/timeit.pyÚreindentMsrcs|‡fdd†}|S)z?Create a timer function. Used if the "statement" is a callable.cs9ˆƒ|ƒ}x|D] }|ƒqW|ƒ}||S)Nr )Z_itZ_timerZ_funcZ_t0Z_iZ_t1)Úsetupr r ÚinnerSs     z_template_func..innerr )rÚfuncrr )rr Ú_template_funcQsrc@s[eZdZdZddedd„Zddd„Zedd „Ze ed d „Z dS) raIClass for timing execution speed of small code snippets. The constructor takes a statement to be timed, an additional statement used for setup, and a timer function. Both statements default to 'pass'; the timer function is platform-dependent (see module doc string). To measure the execution time of the first statement, use the timeit() method. The repeat() method is a convenience to call timeit() multiple times and return a list of results. The statements may contain newlines, as long as they don't contain multi-line string literals. Úpasscs¹||_i‰t|tƒr7t|tƒrXt|tdƒt|d|tdƒnt|tdƒt|dƒ}t|tƒr¶t|dƒ}tjd|d|ddƒ}nCt|ƒrítjd|dd dd ƒ}|ˆd .setupz%stmt is neither a string nor callable)ÚtimerÚ isinstanceÚstrÚcompileÚdummy_src_namerÚtemplateÚformatÚcallableÚ ValueErrorr rrrr)Úselfrrrr Úcoder )rrr Ú__init__ls: !        zTimer.__init__NcCslddl}ddl}|jdk rXt|jƒd|jjdƒtf|jt %.*g secsgš™™™™™É?z raw times:cs g|]}dˆ|f‘qS)z%.*gr )Ú.0Úx)Ú precisionr r ú ;s zmain..z %d loops,g€„.Aièzbest of %d: %.*g usec per loopzbest of %d: %.*g msec per loopzbest of %d: %.*g sec per loop)rHrI)rJrK)rLrM)rNrO)rPrQ)rRrS)rTrU)rVrW)ÚsysÚargvÚgetoptÚerrorÚprintrÚjoinr@Úintr7rCrDZ process_timer>ÚosÚpathÚinsertÚcurdirrr6rr.rÚmin)ÚargsrAr`ZoptsÚerrrrr4rrrEÚoÚarer:r9r[r8ZbestZusecZmsecZsecr )r\r Úmainäsš                                  ,    rnÚ__main__)r>r0r^rCr/Ú__all__r r?r@Z perf_counterrr!rrrrrrnr;Úexitr r r r Ú2s(       ~  e