{"id":16976335,"url":"https://github.com/boisgera/docbench","last_synced_at":"2025-03-21T21:22:18.336Z","repository":{"id":57423190,"uuid":"9114693","full_name":"boisgera/docbench","owner":"boisgera","description":"Doctests Benchmarking","archived":false,"fork":false,"pushed_at":"2018-03-12T18:26:46.000Z","size":76,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-28T11:33:25.069Z","etag":null,"topics":["benchmark","doctest","python"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/boisgera.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-03-30T11:04:24.000Z","updated_at":"2018-03-10T14:39:02.000Z","dependencies_parsed_at":"2022-09-06T07:11:53.935Z","dependency_job_id":null,"html_url":"https://github.com/boisgera/docbench","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/boisgera%2Fdocbench","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/boisgera%2Fdocbench/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/boisgera%2Fdocbench/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/boisgera%2Fdocbench/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/boisgera","download_url":"https://codeload.github.com/boisgera/docbench/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244869911,"owners_count":20523772,"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":["benchmark","doctest","python"],"created_at":"2024-10-14T01:25:47.072Z","updated_at":"2025-03-21T21:22:18.312Z","avatar_url":"https://github.com/boisgera.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build status](https://travis-ci.org/boisgera/docbench.svg?branch=master)](https://travis-ci.org/boisgera/docbench)\n\nDocbench: Doctests Benchmarking\n================================================================================\n\nDocbench is a framework based on [doctest][] to benchmark Python code.\nIf you already are familiar with doctest, \nyou should be able to use docbench in minutes. \n\n[doctest]: http://docs.python.org/2/library/doctest.html\n\n\nExample\n--------------------------------------------------------------------------------\n\nWe will walk you through the use of docbench with a simple use case: \na module that computes prime numbers.\n\nCreate a `primes.py` file and define the `primes` function:\n\n    def primes(n):\n        p = []\n        for i in range(2, n+1):\n            for j in range(2, i):\n                if (i % j == 0):\n                    break\n            else:\n                p.append(i)\n        return p\n\nThe function returns an ordered list of primes numbers up to the number `n`.\nIts implementation is simple but the execution takes a lot of time when `n` grows.\nThe following `sieve` function should return the same result, but performs less\ncomputations and therefore should be faster.\n\n    def sieve(n):\n        p = []\n        for i in range(2, n+1):\n            prime = True\n            for j in p:\n                if j * j \u003e i:\n                    break\n                if (i % j == 0):\n                    prime = False\n                    break\n            if prime:\n                p.append(i)\n        return p\n\n\nDoes it Work?\n--------------------------------------------------------------------------------\n\n**If you already know doctest, you may skip this section.**\n\nTesting this module with doctest is pretty straightforward: create a `test.py`\nfile with a function `test_primes` with no implementation but a doctest \nfor the `primes` function:\n\n    def test_primes():\n        \"\"\"\n        \u003e\u003e\u003e from primes import primes\n        \u003e\u003e\u003e n = 50\n        \u003e\u003e\u003e primes(n)\n        [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]\n        \"\"\"\n\nYou are pretty confident that if `test_primes` doctest works as advertised\nin this doctest, the `primes` function behaves correctly. \nTherefore, you may now test `sieve` against `primes`:\n\n    def test_sieve():\n        \"\"\"\n        \u003e\u003e\u003e from primes import primes, sieve\n        \u003e\u003e\u003e n = 10000\n        \u003e\u003e\u003e primes(n) == sieve(n)\n        True\n        \"\"\"\n\nFinally, add the following boilerplate a the end of your file:\n\n    if __name__ == \"__main__\":\n        import doctest\n        doctest.testmod()\n\nYou are ready to execute this test suite with:\n\n    $ python test.py\n\nNo error displayed ? The `primes` module has successfully passed all tests !\n\n\nIs is Fast?\n--------------------------------------------------------------------------------\n\nCreate a new file name `benchmark.py`. Add functions that act as docbench \nholders for the function `primes` and `sieve`. We rely on the convention \nthat only the time spent in the last statement of any docbench will be \nmeasured, the previous ones are considered setup code.\n\n    def benchmark_primes():\n        \"\"\"\n        \u003e\u003e\u003e from primes import primes\n        \u003e\u003e\u003e n = 10000\n        \u003e\u003e\u003e primes(n)\n        \"\"\"\n\n    def benchmark_sieve():\n        \"\"\"\n        \u003e\u003e\u003e from primes import sieve\n        \u003e\u003e\u003e n = 10000\n        \u003e\u003e\u003e sieve(n)\n        \"\"\"\n\nAdd the following boilerplate at the end of your file:\n\n    if __name__ == \"__main__\":\n        import docbench\n        docbench.benchmod()\n\nRun your benchmark with:\n    \n    $ python benchmark.py\n\nYou should end up with an output similar too:\n\n    Benchmark                  Time            \n    -------------------------  ----------------\n    __main__.benchmark_primes  1.03            \n    __main__.benchmark_sieve   0.00876\n\nIndeed, `sieve` is quite faster than `primes` !\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fboisgera%2Fdocbench","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fboisgera%2Fdocbench","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fboisgera%2Fdocbench/lists"}