o
    à¥µiê  ã                   @   s:   d dl m Z  G dd„ deƒZG dd„ dƒZi Zdd„ ZdS )	é    )Útimec                       s   e Zd Z‡ fdd„Z‡  ZS )Ú
TimerErrorc                    s   || _ tt| ƒ |¡ d S ©N)ÚmessageÚsuperr   Ú__init__)Úselfr   ©Ú	__class__© úU/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/preprocessors/cv/timer.pyr   	   s   zTimerError.__init__)Ú__name__Ú
__module__Ú__qualname__r   Ú__classcell__r   r   r	   r   r      s    r   c                   @   sN   e Zd ZdZddd„Zedd„ ƒZdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ ZdS )ÚTimeraN  A flexible Timer class.
    Example:

    >>> import time
    >>> import mmcv
    >>> with mmcv.Timer():
    >>>     # simulate a code block that will run for 1s
    >>>     time.sleep(1)
    1.000
    >>> with mmcv.Timer(print_tmpl='it takes {:.1f} seconds'):
    >>>     # simulate a code block that will run for 1s
    >>>     time.sleep(1)
    it takes 1.0 seconds
    >>> timer = mmcv.Timer()
    >>> time.sleep(0.5)
    >>> print(timer.since_start())
    0.500
    >>> time.sleep(0.5)
    >>> print(timer.since_last_check())
    0.500
    >>> print(timer.since_start())
    1.000
    TNc                 C   s(   d| _ |r|nd| _|r|  ¡  d S d S )NFz{:.3f})Ú_is_runningÚ
print_tmplÚstart)r   r   r   r   r   r   r   '   s
   ÿzTimer.__init__c                 C   s   | j S )z+bool: indicate whether the timer is running)r   ©r   r   r   r   Ú
is_running-   s   zTimer.is_runningc                 C   s   |   ¡  | S r   )r   r   r   r   r   Ú	__enter__2   s   zTimer.__enter__c                 C   s   t | j |  ¡ ¡ƒ d| _d S )NF)Úprintr   ÚformatÚsince_last_checkr   )r   ÚtypeÚvalueÚ	tracebackr   r   r   Ú__exit__6   s   
zTimer.__exit__c                 C   s    | j s
tƒ | _d| _ tƒ | _dS )zStart the timer.TN)r   r   Ú_t_startÚ_t_lastr   r   r   r   r   :   s   zTimer.startc                 C   s"   | j stdƒ‚tƒ | _| j| j S )zYTotal time since the timer is started.
        Returns (float): Time in seconds.
        útimer is not running)r   r   r   r    r   r   r   r   r   Úsince_startA   s   zTimer.since_startc                 C   s&   | j stdƒ‚tƒ | j }tƒ | _|S )z°Time since the last checking.
        Either :func:`since_start` or :func:`since_last_check` is a checking
        operation.
        Returns (float): Time in seconds.
        r!   )r   r   r   r    )r   Údurr   r   r   r   J   s
   zTimer.since_last_check)TN)r   r   r   Ú__doc__r   Úpropertyr   r   r   r   r"   r   r   r   r   r   r      s    

	r   c                 C   s"   | t vrtƒ t | < dS t |   ¡ S )aÊ  Add check points in a single line.
    This method is suitable for running a task on a list of items. A timer will
    be registered when the method is called for the first time.
    Example:

    >>> import time
    >>> import mmcv
    >>> for i in range(1, 6):
    >>>     # simulate a code block
    >>>     time.sleep(i)
    >>>     mmcv.check_time('task1')
    2.000
    3.000
    4.000
    5.000
    Args:
        timer_id (str): Timer identifier.
    r   )Ú	_g_timersr   r   )Útimer_idr   r   r   Ú
check_timeZ   s   
r(   N)r   Ú	Exceptionr   r   r&   r(   r   r   r   r   Ú<module>   s
   I