B
    âêõbi  ã               @   s0   d Z ddlmZ ddlmZ G dd„ deƒZdS )z{
This is a module that contains basic timer facilities. It's useful for profiling
the performance of a script in "chunks".
é    )Útime)Úlinesepc               @   sJ   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	ddd„Z
dS )ÚTimerzY Timer class. It adds new timers and keeps track of how much time has been
       spent. c             C   s2   dt ƒ  i| _ddi| _dg| _dg| _d| _dS )z Declares the global timer ÚglobalzTotal time taken:zsec.N)r   ÚtimersÚdescriptionsÚactive_timersÚtimer_namesÚunits)Úself© r   ú1lib/python3.7/site-packages/parmed/utils/timer.pyÚ__init__   s
    
zTimer.__init__c             C   s2   || j krdS d| j|< | j  |¡ || j|< dS )z Add a new timer Nr   )r	   r   Úappendr   )r   Ú
timer_nameZdescriptionr   r   r   Ú	add_timer   s
    

zTimer.add_timerc             C   sL   || j krdS || jkr(|  |d| ¡ | j|  tƒ 8  < | j  |¡ dS )z Start the specified timer Nz%s timer)r   r	   r   r   r   r   )r   r   r   r   r   Ústart_timer'   s    

zTimer.start_timerc             C   sH   || j krdS || jkrdS | j|  tƒ 7  < | j | j |¡¡ dS )z End the specified timer N)r	   r   r   r   ÚpopÚindex)r   r   r   r   r   Ú
stop_timer8   s    

zTimer.stop_timerc             C   s(   x"t | jƒdkr"|  | jd ¡ qW dS )z End all of the timers r   N)Úlenr   r   )r   r   r   r   Úend_allH   s    zTimer.end_allc             C   s~   |   ¡  d}| jd dkr&d| _d}n2| jd dkr@d| _d}n| jd dkrXd	| _d}x | jD ]}| j|  |  < q`W d
S )zb Tell Timer we are done, and we can convert units to best 
          human-readable option 
      é   r   i £ Zdaysi€Q i  zhr.é<   zmin.N)r   r   r
   r	   )r   ZtfactorÚtimerr   r   r   ÚdoneO   s    z
Timer.doneTc             C   s4   |  d| j| | j| | jf ¡ |r0|  t¡ dS )z Prints the value of the timer z%-40s %8.3f %sN)Úwriter   r   r
   Úls)r   r   ZoutfileÚnewliner   r   r   Úprint_i   s    zTimer.print_N)T)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r   r   r   r   r      s   
r   N)r#   r   Úosr   r   Úobjectr   r   r   r   r   Ú<module>   s   