Skip to content

Getting Started

This section will help you install the library and basic usage of the stopwatch class.

  • Step. 1: Install the library.

    bash
    poetry add python-stopwatch2
    poetry add python-stopwatch2
    bash
    python3 -m pip install python-stopwatch2
    python3 -m pip install python-stopwatch2
    bash
    py -3 -m pip install python-stopwatch2
    py -3 -m pip install python-stopwatch2
  • Step. 2: Import the Stopwatch class.

    python
    from stopwatch import Stopwatch
    from stopwatch import Stopwatch
  • Step. 3: Create a new Stopwatch object.

    python
    sw = Stopwatch()
    sw = Stopwatch()
  • Step. 4: Do what you want.

    python
    from time import sleep
    sleep(2)
    from time import sleep
    sleep(2)
  • Step. 5: Stop the stopwatch.

    python
    sw.stop()
    sw.stop()
  • Step. 6: Get the elapsed time.

    python
    print(f'Time elapsed: {sw.elapsed}')  # Time elapsed: 2.0031827000002522
    # or
    print(f'Time elapsed: {sw}')  # Time elapsed: 2.00s
    print(f'Time elapsed: {sw.elapsed}')  # Time elapsed: 2.0031827000002522
    # or
    print(f'Time elapsed: {sw}')  # Time elapsed: 2.00s
  • Full example

    python
    from stopwatch import Stopwatch
    from time import sleep
    
    sw = Stopwatch()
    sleep(2)
    sw.stop()
    print(f'Time elapsed: {sw}')  # Time elapsed: 2.00s
    from stopwatch import Stopwatch
    from time import sleep
    
    sw = Stopwatch()
    sleep(2)
    sw.stop()
    print(f'Time elapsed: {sw}')  # Time elapsed: 2.00s

Released under the MIT License.