B
    u9a4                 @   s   d dl Z d dlZd dlZd dlZddddgZdZdZdZejZ	e
Zd	Zd
d ZG dd dZdde	edfddZdde	eedfddZdddddZedkree  dS )    NTimertimeitrepeatdefault_timerz<timeit-src>i@B    z
def inner(_it, _timer{init}):
    {setup}
    _t0 = _timer()
    for _i in _it:
        {stmt}
    _t1 = _timer()
    return _t1 - _t0
c             C   s   |  ddd|  S )N
 )replace)srcindent r   /usr/lib/python3.7/timeit.pyreindentN   s    r   c               @   sL   e Zd ZddedfddZdddZefddZeefd	d
Z	dddZ
dS )r   passNc             C   s   || _ i }|d krt n|}d}t|trJt|td |d }t|d}n*t|rl||d< |d7 }d}d}ntdt|trt|| td t|d	}n&t|r||d
< |d7 }d}ntdt	j
|||d}	|	| _t|	td}
t|
|| |d | _d S )N execr      _setupz, _setup=_setupz_setup()z&setup is neither a string nor callable   Z_stmtz, _stmt=_stmtz_stmt()z%stmt is neither a string nor callable)stmtsetupinitinner)timer_globals
isinstancestrcompiledummy_src_namer   callable
ValueErrortemplateformatr
   r   r   )selfr   r   r   globalsZlocal_nsZ	global_nsr   Z
stmtprefixr
   coder   r   r   __init__d   s6    

zTimer.__init__c             C   sJ   dd l }dd l}| jd k	r:t| jd | jdtf|jt< |j|d d S )Nr   r   )file)	linecache	tracebackr
   lensplitr   cache	print_exc)r#   r'   r(   r)   r   r   r   r-      s    

zTimer.print_excc             C   sB   t d |}t }t  z| || j}W d |r<t  X |S )N)	itertoolsr   gcZ	isenabledZdisabler   r   Zenable)r#   numberitZgcoldZtimingr   r   r   r      s    

zTimer.timeitc             C   s.   g }x$t |D ]}| |}|| qW |S )N)ranger   append)r#   r   r0   ritr   r   r   r      s
    
zTimer.repeatc             C   sT   d}xJx<dD ]4}|| }|  |}|r0||| |dkr||fS qW |d9 }qW d S )N   )r7      r   g?
   )r   )r#   callbackr5   jr0   
time_takenr   r   r   	autorange   s    



zTimer.autorange)N)N)__name__
__module____qualname__r   r&   r-   default_numberr   default_repeatr   r=   r   r   r   r   r   R   s   "
r   c             C   s   t | ||||S )N)r   r   )r   r   r   r0   r$   r   r   r   r      s    c             C   s   t | |||||S )N)r   r   )r   r   r   r   r0   r$   r   r   r   r      s    )_wrap_timerc               s  | d krt jdd  } dd l}y(|| ddddddd	d
ddg	\}} W n4 |jk
rz } zt| td dS d }~X Y nX t}d| pd}d g }t}d}	d ddddddx|D ]\}
}|
dkrt| |
dkr|	| |
dkr|k r|ntdt j
d dS |
dkr0t|}|dkr0d}|
dkr@tj}|
dkr`|	rXd7 |	d7 }	|
dkrttd d! dS qW d|pd}dd l}t jd|j |d k	r||}t|||} dkrd }|	rfd"d#}y||\ }W n   |  dS |	rt  y|| }W n   |  dS fd$d%}|	rltd&d't||  t   fd(d)|D }t|}td*  dkrd+nd,|||f  t|}t|}||d- krdd l}|d.||||f td,d d S )/Nr7   r   zn:u:s:r:tcpvhznumber=zsetup=zrepeat=timeZclockZprocessverbosezunit=helpz#use -h/--help for command line helpr8   r   r   g&.>gư>gMbP?g      ?)ZnsecZusecZmsecZsec   )z-nz--number)z-sz--setup)z-uz--unitz:Unrecognized unit. Please select nsec, usec, msec, or sec.)r'   )z-rz--repeat)z-pz	--process)z-vz	--verbose)z-hz--helpr   )endc                s.   d}| dk}t |j| |rdnd| d d S )Nz%{num} loop{s} -> {secs:.{prec}g} secsr7   sr   )ZnumrI   ZsecsZprec)printr"   )r0   r<   msgplural)	precisionr   r   r:   >  s    zmain.<locals>.callbackc                sb   }|d k	r| }n:dd   D }|jdd x|D ]\}}| |kr:P q:W d | | |f S )Nc             S   s   g | ]\}}||fqS r   r   ).0unitscaler   r   r   
<listcomp>X  s    z-main.<locals>.format_time.<locals>.<listcomp>T)reversez%.*g %s)itemssort)dtrO   rP   Zscales)rM   	time_unitunitsr   r   format_timeR  s    
zmain.<locals>.format_timezraw times: %sz, c                s   g | ]}|  qS r   r   )rN   rU   )r0   r   r   rQ   c  s    zmain.<locals>.<listcomp>z"%d loop%s, best of %d: %s per looprI   r   r   ztThe test results are likely unreliable. The worst time (%s) was more than four times slower than the best time (%s).)sysargvgetopterrorrJ   r   joinrB   intr3   stderrrD   Zprocess_time__doc__ospathinsertcurdirr   r=   r-   r   mapminmaxwarningswarn_explicitUserWarning)argsrC   r[   Zoptserrr   r   r   r   rE   oara   r6   r:   _Zraw_timingsrX   ZtimingsZbestZworstrh   r   )r0   rM   rV   rW   r   main   s    










rp   __main__)N)r/   rY   rD   r.   __all__r   rA   rB   Zperf_counterr   r$   r   r!   r   r   r   r   rp   r>   exitr   r   r   r   <module>3   s*     