{"id":27656065,"url":"https://github.com/ashvardanian/less_slow.py","last_synced_at":"2025-12-26T00:38:45.642Z","repository":{"id":270158968,"uuid":"908345280","full_name":"ashvardanian/less_slow.py","owner":"ashvardanian","description":"Learning how to write \"Less Slow\" code in Python, from numerical micro-kernels to coroutines, ranges, and polymorphic state machines","archived":false,"fork":false,"pushed_at":"2025-03-25T14:02:28.000Z","size":132,"stargazers_count":62,"open_issues_count":3,"forks_count":6,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-19T22:08:16.779Z","etag":null,"topics":["async","benchmark","benchmarking","coroutines","design-patterns","pytest","software-architecture"],"latest_commit_sha":null,"homepage":"https://ashvardanian.com/tags/less-slow/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ashvardanian.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-12-25T20:08:21.000Z","updated_at":"2025-04-19T20:02:56.000Z","dependencies_parsed_at":"2024-12-28T23:17:02.073Z","dependency_job_id":"47deee4d-3abd-44b6-a6de-134762e54fcb","html_url":"https://github.com/ashvardanian/less_slow.py","commit_stats":null,"previous_names":["ashvardanian/less_slow.py"],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ashvardanian%2Fless_slow.py","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ashvardanian%2Fless_slow.py/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ashvardanian%2Fless_slow.py/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ashvardanian%2Fless_slow.py/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ashvardanian","download_url":"https://codeload.github.com/ashvardanian/less_slow.py/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250573351,"owners_count":21452351,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["async","benchmark","benchmarking","coroutines","design-patterns","pytest","software-architecture"],"created_at":"2025-04-24T06:02:32.508Z","updated_at":"2025-12-26T00:38:45.630Z","avatar_url":"https://github.com/ashvardanian.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"# _Less Slow_ Python\n\n\u003e The spiritual little brother of [`less_slow.cpp`](https://github.com/ashvardanian/less_slow.cpp).\n\u003e Assuming Python is used in a different setting than C++, this repository focuses more on scripting, tool integration, and data processing.\n\u003e The benchmarks in this repository don't aim to cover every topic entirely, but they help form a mindset and intuition for performance-oriented software design.\n\nMuch modern code suffers from common pitfalls: bugs, security vulnerabilities, and performance bottlenecks.\nUniversity curricula often teach outdated concepts, while bootcamps oversimplify crucial software development principles.\n\n![Less Slow Python](https://github.com/ashvardanian/ashvardanian/blob/master/repositories/less_slow.py.jpg?raw=true)\n\nThis repository offers practical examples of writing efficient Python code.\nThe topics range from basic micro-kernels executing in a few nanoseconds to more complex constructs involving parallel algorithms, coroutines, and polymorphism. Some of the highlights include:\n\n- A single emoji in a 10K-char ASCII string quadruples its size and also makes `encode('utf-8')` - 40x slower.\n- NumPy matrix multiplication with `int16` is easily 10x-100x slower than `float32` or `float64`, as BLAS can't handle integers... or strides like `[::2, ::2]`.\n- `if value:` is 2x faster than `if len(value) \u003e 0` — Python's truthiness check skips the function call overhead.\n- Async IO, batching, HTTPX, and FastAPI won't save you from slow IO, potentially resulting in 30x slowdowns compared to the already slow Python-native TCP/IP stack.\n- Using callbacks, lambdas, and `yield`-ing functions are much faster than iterator-based routines, unlike Rust and C++.\n- Not all composite structures are equally fast: `namedtuple` is slower than { `dataclass`, `class` } is slower than `dict`.\n- Depending on your design, error handling with status codes can be 50% faster or 2x slower than exceptions.\n- NumPy-based logic can be much slower than `math` functions depending on the shape of the input.\n- JIT compilers like Numba can make your code 2x slower, even if the kernels are precompiled if they are short.\n\nTo read, jump to the `less_slow.py` source file and read the code snippets and comments.\n\n## Reproducing the Benchmarks\n\nIf you are familiar with Python and want to review code and measurements as you read, you can clone the repository and execute the following commands to install the dependencies and run the benchmarks in your local environment.\n\n```sh\ngit clone https://github.com/ashvardanian/less_slow.py.git # Clone the repository\ncd less_slow.py                                            # Change the directory\npip install -r requirements.txt                            # Install the dependencies\npytest less_slow.py                                        # Run all benchmarks\npytest less_slow.py -x -k echo                             # Filter and stop on failure\n```\n\nAlternatively, run the benchmarks in a controlled environment using [`uv`](https://docs.astral.sh/uv/getting-started/installation/).\n\n```sh\nuv run --python=\"3.12\" --no-sync \\\n    --with-requirements requirements.in \\\n    pytest -ra -q less_slow.py\n```\n\nFor `pytest`, the `-r` flag can be used to display a \"short test summary info\" at the end of the test session, making it easy to get a clear picture of all failures in large test suites.\nThe `-ra` variant limits the summary only to failed tests, avoiding \"passed\" and \"passed with outputs\" messages.\n\nFor `uv`, the `--no-sync` flag prevents `uv` from creating a `uv.lock` file or modifying an existing `.venv` folder.\nTo extend the current list of dependencies, update the `requirements.in` file and run `uv sync` to update the environment.\n\n```sh\nuv pip compile requirements.in --universal --output-file requirements.txt\nuv pip sync requirements.txt\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fashvardanian%2Fless_slow.py","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fashvardanian%2Fless_slow.py","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fashvardanian%2Fless_slow.py/lists"}