Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions .github/workflows/codspeed.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: CodSpeed

on:
push:
branches:
- "master"
pull_request:
workflow_dispatch:

permissions:
contents: read
id-token: write

jobs:
benchmarks:
runs-on: ubuntu-latest
steps:
- run: sudo apt-get update && sudo apt-get install -y libhdf5-dev
- uses: actions/checkout@v6
- uses: astral-sh/setup-uv@v7
with:
enable-cache: true
cache-dependency-glob: uv.lock
- uses: actions/setup-python@v6
with:
python-version: "3.14"
allow-prereleases: true
- name: Install dependencies
run: uv sync --group=test
- name: Run benchmarks
uses: CodSpeedHQ/action@v4
with:
mode: simulation
run: uv run pytest benchmarks/ --codspeed
3 changes: 3 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
* [Word Ladder](backtracking/word_ladder.py)
* [Word Search](backtracking/word_search.py)

## Benchmarks
* [Test Benchmarks](benchmarks/test_benchmarks.py)

## Bit Manipulation
* [Binary And Operator](bit_manipulation/binary_and_operator.py)
* [Binary Coded Decimal](bit_manipulation/binary_coded_decimal.py)
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
<a href="https://docs.astral.sh/ruff/formatter/">
<img src="https://img.shields.io/static/v1?label=code%20style&message=ruff&color=black&style=flat-square" height="20" alt="code style: black">
</a>
<a href="https://codspeed.io/Ashishsonavane/Python?utm_source=badge">
<img src="https://img.shields.io/badge/CodSpeed-measured-blue?style=flat-square" height="20" alt="CodSpeed">
</a>

<!-- Short description: -->
<h3>All algorithms implemented in Python - for education 📚</h3>
Expand Down
Empty file added benchmarks/__init__.py
Empty file.
72 changes: 72 additions & 0 deletions benchmarks/test_benchmarks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
"""Benchmarks for sorting and searching algorithms using pytest-codspeed."""

import random

import pytest

from searches.binary_search import binary_search, binary_search_by_recursion
from sorts.bubble_sort import bubble_sort_iterative
from sorts.heap_sort import heap_sort
from sorts.insertion_sort import insertion_sort
from sorts.merge_sort import merge_sort
from sorts.quick_sort import quick_sort
from sorts.selection_sort import selection_sort


def _random_list(size: int, seed: int = 42) -> list[int]:
rng = random.Random(seed)
return rng.sample(range(-size, size), size)


# -- Sorting benchmarks --


@pytest.mark.benchmark
def test_bubble_sort() -> None:
data = _random_list(500)
bubble_sort_iterative(data)


@pytest.mark.benchmark
def test_heap_sort() -> None:
data = _random_list(5000)
heap_sort(data)


@pytest.mark.benchmark
def test_insertion_sort() -> None:
data = _random_list(500)
insertion_sort(data)


@pytest.mark.benchmark
def test_merge_sort() -> None:
data = _random_list(5000)
merge_sort(data)


@pytest.mark.benchmark
def test_quick_sort() -> None:
data = _random_list(5000)
quick_sort(data)


@pytest.mark.benchmark
def test_selection_sort() -> None:
data = _random_list(500)
selection_sort(data)


# -- Search benchmarks --


@pytest.mark.benchmark
def test_binary_search() -> None:
data = sorted(range(10000))
binary_search(data, 4999)


@pytest.mark.benchmark
def test_binary_search_recursive() -> None:
data = sorted(range(10000))
binary_search_by_recursion(data, 4999, 0, len(data) - 1)
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ dependencies = [
[dependency-groups]
test = [
"pytest>=8.4.1",
"pytest-codspeed>=4.3.0",
"pytest-cov>=6",
]

Expand Down
Loading