Tirsvad 790860c465
TirSystem/git-workflows: OpenGrep / opengrep (pull_request) Successful in 2m59s
TirSystem/git-workflows: Publish wiki / publish-wiki (push) Skipped
TirSystem/git-workflows: Release / release (pull_request) Successful in 22s
TirSystem/git-workflows: Validate / validate (pull_request) Successful in 42s
TirSystem/git-workflows: Publish accepted milestones / publish (push) Successful in 33s
TirSystem/git-workflows: Validate / validate (push) Successful in 37s
TirSystem/git-workflows: Sync GitHub mirror metadata / sync-metadata (push) Successful in 14s
TirSystem/git-workflows: OpenGrep / opengrep (push) Successful in 2m39s
Close wiki review RC-008 after publication check
Refs #17
2026-11-06 13:50:21 +08:00
2026-11-06 05:00:27 +01:00

⏱️ Speed Calc Decorator

A Python decorator that measures how many seconds a function takes to run. It is my solution to a lecture exercise from Udemy's 100 Days of Code: The Complete Python Pro Bootcamp, written for fellow course participants and for anyone browsing for ideas. It has no runtime dependencies.

📚 Table of Contents

🧭 Overview

The assignment: write speed_calc_decorator and use it to print how long fast_function and slow_function take. Expected output has this shape:

1695050908.1985211
fast_function run speed: 0.33974480628967285s
slow_function run speed: 2.9590742588043213s

The numbers differ on every machine. The repository is laid out as:

Folder Purpose
src/speed_calc/ The package: the decorator, the two demo functions, constants.py
tests/ pytest tests; they use a fake clock, so they never depend on real time
docs/ Project documents (Business Case, plan, milestones), screenshots and the wiki

📋 Requirements

  • Python 3.13 or newer (python --version).
  • git, to clone the repository.
  • Optional: Doxygen to build the API documentation.

There are no runtime dependencies. pytest is needed only to run the tests.

🛠️ Setup

Create a local virtual environment named .venv in the project folder and activate it. Windows (PowerShell):

python -m venv .venv
.venv\Scripts\Activate.ps1

Linux and macOS:

python3 -m venv .venv
source .venv/bin/activate

Upgrade pip, then install the project with the test tools:

python -m pip install --upgrade pip
python -m pip install -e ".[dev]"

Leave the environment with deactivate. Delete the .venv folder to start over. The .venv folder is ignored by git.

▶️ Run

With the environment active:

python -m speed_calc

It prints the current time, then the run time of fast_function and slow_function. The values depend on your machine.

🪜 Lecture steps

Each step runs on its own with python -m speed_calc.lecture_steps N (N is 1 to 4); step 4 is the same as python -m speed_calc. The screenshots are rendered from the real output of each command by docs/screenshots/generate_screenshots.py.

Step 1: the current time

time.time() returns the seconds since January 1st, 1970.

Step 1: the current time

Step 2: the decorator skeleton

A decorator takes a function and returns a wrapper that calls it. functools.wraps keeps the original name.

Step 2: the decorator skeleton

Step 3: timing the call

The wrapper reads the clock before and after the call and prints the difference.

Step 3: the timing wrapper

Step 4: fast against slow

@speed_calc_decorator above fast_function and slow_function gives the expected output.

Step 4: fast against slow

🧪 Tests

python -m pytest

All tests pass; they use a fake clock, so they do not depend on how fast your machine is.

📖 Documentation

The source uses Doxygen comments. Build the HTML documentation into docs/doxygen/:

doxygen Doxyfile

📄 License

GNU Affero General Public License v3.0, see LICENSE.

📎 Appendix: Project documents

Document What it tells you
AGENTS.md The rules AI agents follow in this repository
Business Case Why the project exists, its scope and success criteria
Stakeholder Analysis Who is involved; the stakeholder IDs used for owners and reviewers
Project Plan The gateways and their schedule
Milestones One document per gateway: Go/No-Go criteria and tasks
Artifact registry Where each artifact type lives, the next version, the PO language and domain
Traceability matrix How the artifacts link and when each was last reviewed
Framework README The framework this project uses
Framework changelog What changed in each framework release

S
Description
Udemy 100 Days of Code, lecture 054: a Python decorator that measures how many seconds a function takes to run. No runtime dependencies, pytest tests, wiki in English, Danish and Bahasa Malaysia.
Readme AGPL-3.0
229 KiB
0 Stars 1 Watchers 0 Forks
Languages
Python 100%