Skip to content

Statistics ​

Source code: stopwatch/statistics.py

Supported Operations

python
len(x)  # get the length of the values
len(x)  # get the length of the values

Initialization ​

python
def __init__(self, values: Optional[List[float]] = None) -> None:
def __init__(self, values: Optional[List[float]] = None) -> None:
  • values: The list of values to be used for the statistics.
    • Type: Optional[List[float]]
    • Default: None

Attributes ​

All attributes of the Statistics class.

mean ​

The mean value in seconds.

Type

Example
python
from stopwatch import Stopwatch
from random import randint
from time import sleep

with Stopwatch() as sw:
    for _ in range(1, 6):
        with sw.lap():
            sleep(randint(1, 10) / 10)
print(sw.statistics.mean)  # 0.5
from stopwatch import Stopwatch
from random import randint
from time import sleep

with Stopwatch() as sw:
    for _ in range(1, 6):
        with sw.lap():
            sleep(randint(1, 10) / 10)
print(sw.statistics.mean)  # 0.5

maximum ​

The maximum value in seconds.

Type

Example
python
from stopwatch import Stopwatch
from random import randint
from time import sleep

with Stopwatch() as sw:
    for _ in range(1, 6):
        with sw.lap():
            sleep(randint(1, 10) / 10)
print(sw.statistics.maximum)  # 0.9
from stopwatch import Stopwatch
from random import randint
from time import sleep

with Stopwatch() as sw:
    for _ in range(1, 6):
        with sw.lap():
            sleep(randint(1, 10) / 10)
print(sw.statistics.maximum)  # 0.9

median ​

The median value in seconds.

Type

Example
python
from stopwatch import Stopwatch
from random import randint
from time import sleep

with Stopwatch() as sw:
    for _ in range(1, 6):
        with sw.lap():
            sleep(randint(1, 10) / 10)
print(sw.statistics.median)  # 0.5
from stopwatch import Stopwatch
from random import randint
from time import sleep

with Stopwatch() as sw:
    for _ in range(1, 6):
        with sw.lap():
            sleep(randint(1, 10) / 10)
print(sw.statistics.median)  # 0.5

minimum ​

The minimum value in seconds.

Type

Example
python
from stopwatch import Stopwatch
from random import randint
from time import sleep

with Stopwatch() as sw:
    for _ in range(1, 6):
        with sw.lap():
            sleep(randint(1, 10) / 10)
print(sw.statistics.minimum)  # 0.1
from stopwatch import Stopwatch
from random import randint
from time import sleep

with Stopwatch() as sw:
    for _ in range(1, 6):
        with sw.lap():
            sleep(randint(1, 10) / 10)
print(sw.statistics.minimum)  # 0.1

total ​

The total value in seconds.

Type

Example
python
from stopwatch import Stopwatch
from random import randint
from time import sleep

with Stopwatch() as sw:
    for _ in range(1, 6):
        with sw.lap():
            sleep(randint(1, 10) / 10)
print(sw.statistics.total)  # 2.5
from stopwatch import Stopwatch
from random import randint
from time import sleep

with Stopwatch() as sw:
    for _ in range(1, 6):
        with sw.lap():
            sleep(randint(1, 10) / 10)
print(sw.statistics.total)  # 2.5

variance ​

The variance value in seconds.

Type

Example
python
from stopwatch import Stopwatch
from random import randint
from time import sleep

with Stopwatch() as sw:
    for _ in range(1, 6):
        with sw.lap():
            sleep(randint(1, 10) / 10)
print(sw.statistics.variance)  # 0.09
from stopwatch import Stopwatch
from random import randint
from time import sleep

with Stopwatch() as sw:
    for _ in range(1, 6):
        with sw.lap():
            sleep(randint(1, 10) / 10)
print(sw.statistics.variance)  # 0.09

Methods ​

All methods of the Statistics class.

add ​

python
def add(self, value: float) -> None:
def add(self, value: float) -> None:

Add a value to the list of values.

Parameters

  • value: The value to be added.

to_dict ​

python
def to_dict(self) -> Dict[str, float]:
def to_dict(self) -> Dict[str, float]:

Get a dictionary with all statistics.

Returns

  • The dictionary with all statistics.

Return type

Example
python
from stopwatch import Stopwatch
from random import randint
from time import sleep

with Stopwatch() as sw:
    for _ in range(1, 6):
        with sw.lap():
            sleep(randint(1, 10) / 10)
print(sw.statistics.to_dict())
# {'mean': 0.5, 'maximum': 0.9, 'median': 0.5, 'minimum': 0.1, 'total': 2.5, 'variance': 0.09}
from stopwatch import Stopwatch
from random import randint
from time import sleep

with Stopwatch() as sw:
    for _ in range(1, 6):
        with sw.lap():
            sleep(randint(1, 10) / 10)
print(sw.statistics.to_dict())
# {'mean': 0.5, 'maximum': 0.9, 'median': 0.5, 'minimum': 0.1, 'total': 2.5, 'variance': 0.09}

Released under the MIT License.