Skip to content

sorts: add cross-algorithm benchmark (sorts/benchmark_sorts.py) - #15284

Merged
cclauss merged 2 commits into
TheAlgorithms:masterfrom
priya-sundaram-dev:sorts-benchmark
Sep 12, 2026
Merged

sorts: add cross-algorithm benchmark (sorts/benchmark_sorts.py)#15284
cclauss merged 2 commits into
TheAlgorithms:masterfrom
priya-sundaram-dev:sorts-benchmark

Conversation

@priya-sundaram-dev

Copy link
Copy Markdown
Contributor

As discussed in #15234, this adds a single place to compare the sorting algorithms in sorts/ on the same datasets, instead of embedding timing code inside individual algorithm modules.

sorts/benchmark_sorts.py:

  • Imports 11 general-purpose comparison sorts from their existing modules (no sort is re-implemented here).
  • Times each on shared, seeded random-integer datasets of a few sizes and prints a small table.
  • Gives every algorithm a fresh copy of the data each run, so in-place sorts don't hand an already-sorted list to the next repetition.
  • Ships doctests for the helpers (is_sorted, all_sorts_agree) that verify every algorithm agrees with Python's built-in sorted; the timing itself stays under if __name__ == "__main__" so it doesn't slow down the doctest suite.

Run it from the repo root:

python -m sorts.benchmark_sorts

Example output:

algorithm                      100        1000        3000
----------------------------------------------------------
comb_sort                   0.0001      0.0022      0.0106
heap_sort                   0.0003      0.0018      0.0067
merge_sort                  0.0002      0.0023      0.0081
quick_sort                  0.0001      0.0012      0.0033
shell_sort                  0.0001      0.0011      0.0042
bubble_sort                 0.0003      0.0384      0.3664
...

Happy to add/remove algorithms or adjust the sizes/columns to taste.

@algorithms-keeper algorithms-keeper Bot added awaiting reviews This PR is ready to be reviewed invalid labels Sep 11, 2026
@algorithms-keeper algorithms-keeper Bot removed the awaiting reviews This PR is ready to be reviewed label Sep 11, 2026
@cclauss cclauss reopened this Sep 12, 2026
@algorithms-keeper algorithms-keeper Bot added the require tests Tests [doctest/unittest/pytest] are required label Sep 12, 2026

@algorithms-keeper algorithms-keeper Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

Comment thread sorts/benchmark_sorts.py Outdated
Comment thread sorts/benchmark_sorts.py
@cclauss cclauss removed the invalid label Sep 12, 2026
@TheAlgorithms TheAlgorithms deleted a comment from algorithms-keeper Bot Sep 12, 2026

@cclauss cclauss left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super cool!

Comment thread sorts/benchmark_sorts.py Outdated
Comment thread sorts/benchmark_sorts.py
Comment thread sorts/benchmark_sorts.py
Comment thread sorts/benchmark_sorts.py
@algorithms-keeper algorithms-keeper Bot added the awaiting changes A maintainer has requested changes to this PR label Sep 12, 2026
Updated the benchmark_sorts.py script to improve type safety and added a Comparable protocol for generic sorting. Adjusted comments for clarity and fixed minor formatting issues.

@algorithms-keeper algorithms-keeper Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

Comment thread sorts/benchmark_sorts.py


class Comparable(Protocol):
def __lt__(self, other: object, /) -> bool: ...

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file sorts/benchmark_sorts.py, please provide doctest for the function __lt__

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LOL!!!

Comment thread sorts/benchmark_sorts.py
return timings


def main() -> None:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file sorts/benchmark_sorts.py, please provide doctest for the function main

@algorithms-keeper algorithms-keeper Bot added awaiting reviews This PR is ready to be reviewed and removed awaiting changes A maintainer has requested changes to this PR labels Sep 12, 2026
@algorithms-keeper algorithms-keeper Bot removed the awaiting reviews This PR is ready to be reviewed label Sep 12, 2026
@cclauss
cclauss merged commit 341e1da into TheAlgorithms:master Sep 12, 2026
6 checks passed
@priya-sundaram-dev

Copy link
Copy Markdown
Contributor Author

Thanks for the merge! 😄 The keeper flagging main() right as it went in is a fun coda. I took a look at require_doctest.py — init is already exempt via the INIT check in visit_FunctionDef, so teaching it to skip main() too is a small change plus a couple of VALID/INVALID cases. Happy to open a PR on algorithms-keeper if that would be welcome — just say the word.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

require tests Tests [doctest/unittest/pytest] are required

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants