File size: 1,735 Bytes
1bf41f9
8ccf878
f988527
1bf41f9
 
8ccf878
1bf41f9
8ccf878
 
 
447b452
1bf41f9
8ccf878
f988527
1bf41f9
 
afb0c77
271d94c
8ccf878
 
 
afb0c77
 
1bf41f9
f988527
 
 
1bf41f9
 
afb0c77
8ccf878
afb0c77
8ccf878
afb0c77
 
 
 
1bf41f9
 
f988527
 
 
 
 
 
 
1bf41f9
8ccf878
 
 
1bf41f9
8ccf878
afb0c77
1bf41f9
 
8ccf878
 
 
 
1bf41f9
 
 
 
afb0c77
8ccf878
 
 
afb0c77
 
 
 
1bf41f9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# -*- coding: utf-8 -*-
"""ใƒฆใƒผใƒ†ใ‚ฃใƒชใƒ†ใ‚ฃ"""
from dataclasses import dataclass
import time


def get_package_version() -> str:
    """

    ใƒใƒผใ‚ธใƒงใƒณๆƒ…ๅ ฑ

    """
    return '0.0.8'


@dataclass
class Stopwatch:
    """

    ็ตŒ้Žๆ™‚้–“ใ‚’่จˆๆธฌใ™ใ‚‹ใŸใ‚ใฎใ‚ฏใƒฉใ‚นใ€‚

    Example:

        from src.utils import Stopwatch



        watch = Stopwatch.start_new()

        ### ่จˆๆธฌใ™ใ‚‹ๅ‡ฆ็†

        print(f"{watch.elapsed:.3f}")

    """
    _start_time: float = 0
    _elapsed: float = 0
    _is_running: bool = False

    @property
    def elapsed(self) -> float:
        """

        ็ตŒ้Žๆ™‚้–“ใ‚’ๅ–ๅพ—ใ—ใพใ™ใ€‚

        """
        if self._is_running:
            end_time = time.perf_counter()
            self._elapsed = end_time - self._start_time

        return self._elapsed

    @property
    def is_running(self) -> bool:
        """

        ๅฎŸ่กŒไธญใ‹ใฉใ†ใ‹ใ‚’ๅ–ๅพ—ใ—ใพใ™ใ€‚

        """
        return self._is_running

    def start(self) -> None:
        """

        ่จˆๆธฌใ‚’้–‹ๅง‹ใ—ใพใ™ใ€‚

        """
        self._start_time = time.perf_counter()
        self._elapsed = 0
        self._is_running = True

    @classmethod
    def start_new(cls):
        """

        ใ‚นใƒˆใƒƒใƒ—ใ‚ฆใ‚ฉใƒƒใƒใ‚’็”Ÿๆˆใ—่จˆๆธฌใ‚’้–‹ๅง‹ใ—ใพใ™ใ€‚

        """
        stopwatch = Stopwatch()
        stopwatch.start()
        return stopwatch

    def stop(self) -> float:
        """

        ่จˆๆธฌใ‚’็ต‚ไบ†ใ—ใพใ™ใ€‚

        """
        if self._is_running:
            end_time = time.perf_counter()
            self._elapsed = end_time - self._start_time
            self._is_running = False
        return self._elapsed