<s> """<STR_LIT>""" <EOL> import gc <EOL> import sys <EOL> import time <EOL> try : <EOL> import itertools <EOL> except ImportError : <EOL> itertools = None <EOL> __all__ = [ "<STR_LIT>" ] <EOL> dummy_src_name = "<STR_LIT>" <EOL> default_number = <NUM_LIT> <EOL> default_repeat = <NUM_LIT:3> <EOL> if sys . platform == "<STR_LIT:win32>" : <EOL> default_timer = time . clock <EOL> else : <EOL> default_timer = time . time <EOL> template = """<STR_LIT>""" <EOL> def reindent ( src , indent ) : <EOL> """<STR_LIT>""" <EOL> return src . replace ( "<STR_LIT:\n>" , "<STR_LIT:\n>" + "<STR_LIT:U+0020>" * indent ) <EOL> def _template_func ( setup , func ) : <EOL> """<STR_LIT>""" <EOL> def inner ( _it , _timer ) : <EOL> setup ( ) <EOL> _t0 = _timer ( ) <EOL> for _i in _it : <EOL> func ( ) <EOL> _t1 = _timer ( ) <EOL> return _t1 - _t0 <EOL> return inner <EOL> class Timer : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , stmt = "<STR_LIT>" , setup = "<STR_LIT>" , timer = default_timer ) : <EOL> """<STR_LIT>""" <EOL> self . timer = timer <EOL> ns = { } <EOL> if isinstance ( stmt , basestring ) : <EOL> stmt = reindent ( stmt , <NUM_LIT:8> ) <EOL> if isinstance ( setup , basestring ) : <EOL> setup = reindent ( setup , <NUM_LIT:4> ) <EOL> src = template % { '<STR_LIT>' : stmt , '<STR_LIT>' : setup } <EOL> elif callable ( setup ) : <EOL> src = template % { '<STR_LIT>' : stmt , '<STR_LIT>' : '<STR_LIT>' } <EOL> ns [ '<STR_LIT>' ] = setup <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> self . src = src <EOL> code = compile ( src , dummy_src_name , "<STR_LIT>" ) <EOL> exec code in globals ( ) , ns <EOL> self . inner = ns [ "<STR_LIT>" ] <EOL> elif callable ( stmt ) : <EOL> self . src = None <EOL> if isinstance ( setup , basestring ) : <EOL> _setup = setup <EOL> def setup ( ) : <EOL> exec _setup in globals ( ) , ns <EOL> elif not callable ( setup ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> self . inner = _template_func ( setup , stmt ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> def print_exc ( self , file = None ) : <EOL> """<STR_LIT>""" <EOL> import linecache , traceback <EOL> if self . src is not None : <EOL> linecache . cache [ dummy_src_name ] = ( len ( self . src ) , <EOL> None , <EOL> self . src . split ( "<STR_LIT:\n>" ) , <EOL> dummy_src_name ) <EOL> traceback . print_exc ( file = file ) <EOL> def timeit ( self , number = default_number ) : <EOL> """<STR_LIT>""" <EOL> if itertools : <EOL> it = itertools . repeat ( None , number ) <EOL> else : <EOL> it = [ None ] * number <EOL> gcold = gc . isenabled ( ) <EOL> gc . disable ( ) <EOL> timing = self . inner ( it , self . timer ) <EOL> if gcold : <EOL> gc . enable ( ) <EOL> return timing <EOL> def repeat ( self , repeat = default_repeat , number = default_number ) : <EOL> """<STR_LIT>""" <EOL> r = [ ] <EOL> for i in range ( repeat ) : <EOL> t = self . timeit ( number ) <EOL> r . append ( t ) <EOL> return r <EOL> def timeit ( stmt = "<STR_LIT>" , setup = "<STR_LIT>" , timer = default_timer , <EOL> number = default_number ) : <EOL> """<STR_LIT>""" <EOL> return Timer ( stmt , setup , timer ) . timeit ( number ) <EOL> def repeat ( stmt = "<STR_LIT>" , setup = "<STR_LIT>" , timer = default_timer , <EOL> repeat = default_repeat , number = default_number ) : <EOL> """<STR_LIT>""" <EOL> return Timer ( stmt , setup , timer ) . repeat ( repeat , number ) <EOL> def main ( args = None ) : <EOL> """<STR_LIT>""" <EOL> if args is None : <EOL> args = sys . argv [ <NUM_LIT:1> : ] <EOL> import getopt <EOL> try : <EOL> opts , args = getopt . getopt ( args , "<STR_LIT>" , <EOL> [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT:time>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> except getopt . error , err : <EOL> print err <EOL> print "<STR_LIT>" <EOL> return <NUM_LIT:2> <EOL> timer = default_timer <EOL> stmt = "<STR_LIT:\n>" . join ( args ) or "<STR_LIT>" <EOL> number = <NUM_LIT:0> <EOL> setup = [ ] <EOL> repeat = default_repeat <EOL> verbose = <NUM_LIT:0> <EOL> precision = <NUM_LIT:3> <EOL> for o , a in opts : <EOL> if o in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> number = int ( a ) <EOL> if o in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> setup . append ( a ) <EOL> if o in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> repeat = int ( a ) <EOL> if repeat <= <NUM_LIT:0> : <EOL> repeat = <NUM_LIT:1> <EOL> if o in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> timer = time . time <EOL> if o in ( "<STR_LIT:-c>" , "<STR_LIT>" ) : <EOL> timer = time . clock <EOL> if o in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> if verbose : <EOL> precision += <NUM_LIT:1> <EOL> verbose += <NUM_LIT:1> <EOL> if o in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> print __doc__ , <EOL> return <NUM_LIT:0> <EOL> setup = "<STR_LIT:\n>" . join ( setup ) or "<STR_LIT>" <EOL> import os <EOL> sys . path . insert ( <NUM_LIT:0> , os . curdir ) <EOL> t = Timer ( stmt , setup , timer ) <EOL> if number == <NUM_LIT:0> : <EOL> for i in range ( <NUM_LIT:1> , <NUM_LIT:10> ) : <EOL> number = <NUM_LIT:10> ** i <EOL> try : <EOL> x = t . timeit ( number ) <EOL> except : <EOL> t . print_exc ( ) <EOL> return <NUM_LIT:1> <EOL> if verbose : <EOL> print "<STR_LIT>" % ( number , precision , x ) <EOL> if x >= <NUM_LIT> : <EOL> break <EOL> try : <EOL> r = t . repeat ( repeat , number ) <EOL> except : <EOL> t . print_exc ( ) <EOL> return <NUM_LIT:1> <EOL> best = min ( r ) <EOL> if verbose : <EOL> print "<STR_LIT>" , "<STR_LIT:U+0020>" . join ( [ "<STR_LIT>" % ( precision , x ) for x in r ] ) <EOL> print "<STR_LIT>" % number , <EOL> usec = best * <NUM_LIT> / number <EOL> if usec < <NUM_LIT:1000> : <EOL> print "<STR_LIT>" % ( repeat , precision , usec ) <EOL> else : <EOL> msec = usec / <NUM_LIT:1000> <EOL> if msec < <NUM_LIT:1000> : <EOL> print "<STR_LIT>" % ( repeat , precision , msec ) <EOL> else : <EOL> sec = msec / <NUM_LIT:1000> <EOL> print "<STR_LIT>" % ( repeat , precision , sec ) <EOL> return None <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> sys . exit ( main ( ) ) </s>
